Re: memory leak?



On Fri, 27 Jul 2007 21:33:51 -0000, wanwan <ericwan78@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
.



Relevant Pages

  • Re: memory leak?
    ... pointing at a memory location. ... So freehad no effect on myPtr. ... The pointer value should be unusable at that point, but that is not something you can see. ... with the same pointer variable as the one I use to allocate memory? ...
    (microsoft.public.vc.mfc)
  • Re: memory leak?
    ... So freehad no effect on myPtr. ... The function free() is part of the C Standard Library, ... That doesn't mean it didn't free the memory, ... It caused memory leak. ...
    (microsoft.public.vc.mfc)
  • Re: [PATCH RT 00/02] SLOB optimizations
    ... > Linux's memory management is starting to show it's age. ... The slab allocator is indeed complex, messy, and hard to understand. ... +#undef CACHE +}; +EXPORT_SYMBOL; + +struct cache_names { ... +void *kzalloc+{ ...
    (Linux-Kernel)
  • [PATCH 1/3] kmemcheck: add the kmemcheck core
    ... Subject: kmemcheck: add the kmemcheck core ... detects use of uninitialized memory. ... +static unsigned int error_count; ... +static void ...
    (Linux-Kernel)
  • [RFC][PATCH 08/12] memory hotplug: sysfs and add/remove functions
    ... This adds generic memory add/remove and supporting functions ... extern int devices_init; ... +void unregister_memory_notifier ... +/* reasonably generic interface to expand the physical pages in a zone */ ...
    (Linux-Kernel)

Loading