Re: memory leak?



It could be that for some reason, CRT cant find now that there's a memory
leak, but there's still one out there... I think that you should temporarily
switch back to malloc() and free(), find out where the leak is coming from,
then switch back to new and delete.

Anyway for the allocation of chars there shouldnt be any difference between
malloc or new,
because it is not a class with constructors and destructors, it is just a
single byte of memory.

I think you are using the new keywork incorrectly, maybe you are not
allocation the array of chars correctly, and then you dont have a memory
leak. Or maybe you were using malloc incorrectly and now its right...

But do not assume that you have solved your problem, you should investigate
this memory leak deeply before you continue with writing your program.

Daniel C. Gindi

"wanwan" <ericwan78@xxxxxxxxx> wrote in message
news:1185620560.998495.188890@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Jul 27, 8:04 pm, "Doug Harrison [MVP]" <d...@xxxxxxxx> wrote:
On Fri, 27 Jul 2007 21:33:51 -0000, wanwan <ericwa...@xxxxxxxxx> wrote:
I ran my program is debug mode. I marked a breakpoint at the line
free(myPtr). I found out after stepping over the line, myPtr was still
pointing at a memory location. So free() had no effect on myPtr.

The function free() is part of the C Standard Library, and since C has
only
pass-by-value, it is not possible for free(p) to modify the argument p.
That doesn't mean it didn't free the memory, though.

One thing to note is that in my program, I used another pointer
variable to allocate memory. Later on, the block of memory was passed
to myPtr as a pointer. Is there a rule that requires me to free memory
with the same pointer variable as the one I use to allocate memory? It
looks like the case to me.

No, that's not the case. That is, this is fine:

void* p = malloc(1);
void* q = p;
free(q);
// Now p and q are considered indeterminate, and the only legal thing
// you can do with them is assign them new values.

At what time (from what function), and in what context (EXE or DLL), are
you calling free? How are you linking to MFC (static or dynamic)? What is
the exact text of the leak report (copy it out of the debugger output
window)?

--
Doug Harrison
Visual C++ MVP

I found a solution that doesn't make too much sense to me.

Originally I used malloc and free in treating the use of memory in the
char pointer like so:
char* ca = (char*) malloc(1234*sizeof(char));
...
free(ca);

.... It caused memory leak.

After playing with my code, I change to using new and delete. The
memory leak is now gone.

I thought they are the same thing. .Anyway, hope someone else can make
use of my findings.



.



Relevant Pages

  • Re: malloc questions in C
    ... malloc() or one of the similar functions, it will be available until you call free. ... A lot of C implementations will also free this memory for you when your program exits, ... /* a) myhappyfunmodule has an internal memory leak */ ...
    (comp.lang.c)
  • Re: MDAC memory leak
    ... Most libraries place the decision of when to free ... There's a capability of breaking on a particular memory allocation, ... leak 500 objects, on the second test I leak 3, because I fixed the bug). ... "App shows memory leak on some machines." ...
    (microsoft.public.vc.mfc)
  • Re: memory leak?
    ... leak, but there's still one out there... ... switch back to malloc() and free, find out where the leak is coming from, ... single byte of memory. ... with the same pointer variable as the one I use to allocate memory? ...
    (microsoft.public.vc.mfc)
  • Re: MDAC memory leak
    ... Also when we used some of the memory leak tools suggested on microsoft site ... A mutex is a considerably less efficient synchronization ... "App shows memory leak on some machines." ...
    (microsoft.public.vc.mfc)
  • Re: Dynamic Allocation Problem...
    ... The only legitimate failure from malloc() is a NULL pointer, and, ... > mallocto allocate memory for the Pointer variables. ...
    (microsoft.public.win32.programmer.kernel)

Loading