在这种情况下内存泄漏?一遍又一遍地使用新关键字
Memory leak in this situation? Using new keyword over and over
我对内存泄漏非常谨慎,所以我想我会对此进行验证。在下面的例子中会不会有内存泄漏?我的直觉说是的。
class Handler // Class definition
{public:
~Handler();
int* ptrToInts;
};
Handler::~Handler() // Class destructor
{
delete[] ptrToInts;
}
Handler handler; // Global object
void aFunction()
{
handler.ptrToInts = new int[20];
}
int main()
{
bool quit = false;
while(!quit)
{
aFunction();
}
return 0;
}
ptrToInts 会在 heapeach 时间在单独的内存中创建 20 个单独的新整数吗?
还有一个问题,如果没有析构函数,动态分配的内存会被释放吗?好像 class 的生命周期就是程序的持续时间,它会清理所有 "new" 内存吗?
编辑:感谢您的回答。我问这个的原因是因为基本上每次为原始输入调用 WndProc 时,我都试图绕过调用 new 和 delete ,这就是 MSDN 告诉你这样做的方式。看起来效率很低。
只有 delete[]
释放由 new
分配的内存。并且每次使用 new
时,您 需要 一个 delete
.
另一个问题,基于Documentation:
MyClass * p1 = new MyClass[5]; // allocates and constructs five objects
一旦您重新分配指针而不使用 delete[]
取消分配堆上已分配的内存,就会造成内存泄漏。如果您循环 aFunction()
,就会发生这种情况,因为它会在每次调用时重新分配指针。
至于你的第二个问题,你的析构函数只会delete[]
最后一个数组赋值给指针。
是的,当您多次调用该函数时会发生内存泄漏,而没有在每次调用后显式释放 handler.ptrToInts
;
void aFunction()
{
handler.ptrToInts = new int[20];
}
//-----somewhere we see the caller
while(!quit)
{
aFunction();
}
然而,这是一个检测泄漏的小案例......你应该学会使用 Leak detectors and static analyzers。
见How do you detect/avoid Memory leaks in your (Unmanaged) code?
当然有内存泄漏。您在
中分配整数
void aFunction()
{
handler.ptrToInts = new int[20];
}
无需先释放旧整数,例如
void aFunction()
{
delete [] handler.ptrToInts;
handler.ptrToInts = new int[20];
}
会做。
调用aFunction()
将导致"infinite" 内存分配。
而你的析构函数,它只释放最后分配的整数,甚至从未被调用。
为什么您的处理程序不管理它自己的内存?
在对象外部分配内存并在对象内部释放内存是非常糟糕的做法,反之亦然。
为什么不以这种方式实现处理程序class:
class Handler
{
public:
Handler();
~Handler();
void aMethod();
private:
int* ptrToInts;
};
Handler::Handler() {
handler.ptrToInts = new int[20];
}
Handler::~Handler() {
delete[] ptrToInts;
}
void Handler::aMethod() {
delete[] ptrToInts;
handler.ptrToInts = new int[20];
}
int main() {
bool quit = false;
Handler handler;
while(!quit) {
handler.aMethod();
}
}
我对内存泄漏非常谨慎,所以我想我会对此进行验证。在下面的例子中会不会有内存泄漏?我的直觉说是的。
class Handler // Class definition
{public:
~Handler();
int* ptrToInts;
};
Handler::~Handler() // Class destructor
{
delete[] ptrToInts;
}
Handler handler; // Global object
void aFunction()
{
handler.ptrToInts = new int[20];
}
int main()
{
bool quit = false;
while(!quit)
{
aFunction();
}
return 0;
}
ptrToInts 会在 heapeach 时间在单独的内存中创建 20 个单独的新整数吗?
还有一个问题,如果没有析构函数,动态分配的内存会被释放吗?好像 class 的生命周期就是程序的持续时间,它会清理所有 "new" 内存吗?
编辑:感谢您的回答。我问这个的原因是因为基本上每次为原始输入调用 WndProc 时,我都试图绕过调用 new 和 delete ,这就是 MSDN 告诉你这样做的方式。看起来效率很低。
只有 delete[]
释放由 new
分配的内存。并且每次使用 new
时,您 需要 一个 delete
.
另一个问题,基于Documentation:
MyClass * p1 = new MyClass[5]; // allocates and constructs five objects
一旦您重新分配指针而不使用 delete[]
取消分配堆上已分配的内存,就会造成内存泄漏。如果您循环 aFunction()
,就会发生这种情况,因为它会在每次调用时重新分配指针。
至于你的第二个问题,你的析构函数只会delete[]
最后一个数组赋值给指针。
是的,当您多次调用该函数时会发生内存泄漏,而没有在每次调用后显式释放 handler.ptrToInts
;
void aFunction()
{
handler.ptrToInts = new int[20];
}
//-----somewhere we see the caller
while(!quit)
{
aFunction();
}
然而,这是一个检测泄漏的小案例......你应该学会使用 Leak detectors and static analyzers。
见How do you detect/avoid Memory leaks in your (Unmanaged) code?
当然有内存泄漏。您在
中分配整数void aFunction()
{
handler.ptrToInts = new int[20];
}
无需先释放旧整数,例如
void aFunction()
{
delete [] handler.ptrToInts;
handler.ptrToInts = new int[20];
}
会做。
调用aFunction()
将导致"infinite" 内存分配。
而你的析构函数,它只释放最后分配的整数,甚至从未被调用。
为什么您的处理程序不管理它自己的内存?
在对象外部分配内存并在对象内部释放内存是非常糟糕的做法,反之亦然。
为什么不以这种方式实现处理程序class:
class Handler
{
public:
Handler();
~Handler();
void aMethod();
private:
int* ptrToInts;
};
Handler::Handler() {
handler.ptrToInts = new int[20];
}
Handler::~Handler() {
delete[] ptrToInts;
}
void Handler::aMethod() {
delete[] ptrToInts;
handler.ptrToInts = new int[20];
}
int main() {
bool quit = false;
Handler handler;
while(!quit) {
handler.aMethod();
}
}