Re: Memory Leak detection with HeapAlloc ... ?
- From: Philip McGraw <pmcgraw@xxxxxxxxxxxxx>
- Date: Tue, 17 Feb 2009 18:15:54 -0500
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
.
- Follow-Ups:
- Re: Memory Leak detection with HeapAlloc ... ?
- From: Alex Blekhman
- Re: Memory Leak detection with HeapAlloc ... ?
- From: Martin T.
- Re: Memory Leak detection with HeapAlloc ... ?
- References:
- Memory Leak detection with HeapAlloc ... ?
- From: Martin T.
- Re: Memory Leak detection with HeapAlloc ... ?
- From: Alex Blekhman
- Re: Memory Leak detection with HeapAlloc ... ?
- From: Martin T.
- Memory Leak detection with HeapAlloc ... ?
- Prev by Date: Re: Genuinely local objects (from Stroustrup's FAQ)
- Next by Date: InterlockedCompareExchange which exchanges the value when values are different
- Previous by thread: Re: Memory Leak detection with HeapAlloc ... ?
- Next by thread: Re: Memory Leak detection with HeapAlloc ... ?
- Index(es):
Relevant Pages
|