Re: Memory Leak detection with HeapAlloc ... ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Martin T. wrote:
Alex Blekhman wrote:
"Martin T." wrote:
... So the question is, is there any possibility of detecting leaks of memory allocated with HeapAlloc ??

Your code won't detect memory leaks from HeapAlloc because it checks only those allocations taht were made via CRT. HeapAlloc belongs to Platform SDK, so by calling it directly you circumvent CRT allocation bookeeping.

However, There are tools to detect meory leaks that were allocated with PSDK functions, too:

KB286470 - "How to use Pageheap.exe in Windows XP, Windows 2000, and Windows Server 2003"
http://support.microsoft.com/kb/286470

KB264471 - "How to use the PageHeap utility to detect memory errors in a Microsoft Visual C++ project"
http://support.microsoft.com/kb/264471



Cheers. Looks promising, only ... anyone knows of a solution where one could enable a page heap as described in the articles from sourcecode?
That way one could just enable the stuff in the debug version and be done with it ...
(Although I have a feeling the API is undocumented: "Pageheap.exe (...) enables a verification layer that already exists in the Ntdll.dll system libraries in Windows")

br,
Martin

Martin,

Which type of memory errors you are really trying to find: leaks
or overwrites (or both)? Your original question was about leaks,
but Alex's reply about page heap answers the problem of finding
heap memory overwrites.

Page heap is quite useful for finding heap memory overwrites which
would lead to heap corruption and access violation crashes, but it
is not the correct tool for finding leaks. While an overwrite is
unintentionally modifying a memory object beyond its designed size
limits, a memory leak occurs when memory is dynamically allocated
from a heap but is never freed.

While many tools exist for finding memory leaks, UMDH is the one
available in the same "Debugging Tools for Windows" Microsoft
install set as page heap. The gflags program is used for enabling
both page heap and the "User Stack Trace" (+ust) option used by
UMDH. Older documentation refers to a pageheap program, but the
syntax is now "gflags -p" rather than pageheap.

Both page heap and user stack traces are controlled by registry
flag values under
"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options"
so your question wish to enabling and disabling memory debugging
at runtime from source code would be possible by setting some
registry values via the appropriate win32 API calls. You can
easily use gflags to set the flags you want for the image you
are debugging, and then see what VerifierFlags, GlobalFlag, and
PageHeapFlags get set in the registry. It is also possible to
make gflags changes to the registry using .reg files, of course.

Unfortunately, the settings for using page heap and UMDH are
mutually exclusive: you can use one or the other, but not both
simultaneously for a given program. This is likely due to the
different heap structure used when page heap is enabled, because
the same type of user stack trace database maintained for UMDH
is also maintained when page heap is enabled, but the UMDH
program cannot do leak detection when full page heap is enabled.

Another set of Microsoft tools for finding both memory
overwrite and memory leak errors is DebugDiag and LeakDiag.
DebugDiag can be used to run programs with page heap flags
enabled, but the LeakDiag uses a different mechanism than
UMDH for detecting memory leaks. You may find DebugDiag's
GUI easier to use than the "Debugging Tools for Windows"
gflags -p and umdh command line interface.

Hope this helps,
Philip McGraw

References:
Debugging Tools for Windows download for 32-bit:
http://www.microsoft.com/whdc/devtools/debugging/installx86.Mspx

Debugging Tools for Windows newsgroup:
news://msnews.microsoft.com/microsoft.public.windbg

DebugDiag 1.1 download:
http://www.microsoft.com/downloads/details.aspx?FamilyID=28bd5941-c458-46f1-b24d-f60151d875a3&displaylang=en
.



Relevant Pages

  • heapcreate/heapfree and reserved segments
    ... uncommitted memory available within the address space of the process. ... When we analyze heap utilisation in process dumps from the site we ... an initial 256KB segment is reserved. ... allocations since the amount of VM address space overhead that will be ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Memory is fragmented even though everything is deallocated
    ... I am running Windows XP indeed. ... Plus, if I destroy the process heap, will I be able to allocate on the ... My guess is that deallocated memory is not released by the ... for small allocations only, since the huge chunk of memory allocated ...
    (microsoft.public.vc.language)
  • Re: Heap allocation without HeapAlloc ()
    ... There are ways to leak memory that don't involve the heap. ... My strategy has been to set a breakpoint in HeapAlloc ... > to try and witness which allocations the 3rd party code is making. ...
    (microsoft.public.vc.debugger)
  • Running DLLs from mixed MsDev versions
    ... and there are a few methods in those DLL that are ... allocating memory, which I as a client need to free. ... allocations work, nor is there a way for us to receive these DLLs ... same heap as my software? ...
    (comp.windows.misc)
  • Running DLLs from mixed MsDev versions
    ... and there are a few methods in those DLL that are ... allocating memory, which I as a client need to free. ... allocations work, nor is there a way for us to recieve these DLLs ... same heap as my software? ...
    (comp.programming)