Memory leak problem.
-
Hi all. I have a memory leak on a C++ MFC app I'm trying to solve. In the problematc part of code there are some function calls as follows: void functionA() { functionX(new ClassConstructor()); } Since this unmanaged code, is it safe to do that without freeing the memory allocated by the new operator, or it will be released after exiting the functionA scope? Shouldn't be something like the following a good solution? void functionA() { ClassConstructor *obj = new ClassContructor(); functionX(obj); delete obj; } Thanks in advance :)
-
Hi all. I have a memory leak on a C++ MFC app I'm trying to solve. In the problematc part of code there are some function calls as follows: void functionA() { functionX(new ClassConstructor()); } Since this unmanaged code, is it safe to do that without freeing the memory allocated by the new operator, or it will be released after exiting the functionA scope? Shouldn't be something like the following a good solution? void functionA() { ClassConstructor *obj = new ClassContructor(); functionX(obj); delete obj; } Thanks in advance :)
The object instantiated by the
new
needs a matchingdelete
. It's not clear who's responsible for deleting it from what you've told us, but someone needs to. A comment on this code you posted:sirtimid wrote:
void functionA() { ClassConstructor *obj = new ClassContructor(); functionX(obj); delete obj; }
In this case there's no reason to use the heap. Do something like this instead:
void functionA()
{
ClassConstructor obj;
functionX(&obj);
}Steve
-
The object instantiated by the
new
needs a matchingdelete
. It's not clear who's responsible for deleting it from what you've told us, but someone needs to. A comment on this code you posted:sirtimid wrote:
void functionA() { ClassConstructor *obj = new ClassContructor(); functionX(obj); delete obj; }
In this case there's no reason to use the heap. Do something like this instead:
void functionA()
{
ClassConstructor obj;
functionX(&obj);
}Steve