Quantcast
Channel: Visual Leak Detector for Visual C++ 2008-2015
Viewing all articles
Browse latest Browse all 704

Commented Unassigned: HeapDestroy without HeapFree really leaks memory? [10580]

$
0
0
Hi VLD developers, I'm a developer of [MinHook](http://www.example.com).

A user of MinHook reported a memory leak. In short, VLD reports a leak when calling HeapDestroy without HeapFree.
Look at the test code below:
```
#include <windows.h>
#include <vld.h>

int main()
{
HANDLE hHeap = HeapCreate(0, 0, 0);
LPVOID pHeap = HeapAlloc(hHeap, 0, 1024);

// Skip HeapFree intentionally.

HeapDestroy(hHeap); // returns TRUE

return 0;
}
```
It causes an error like this:
```
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x010D05B8: 1024 bytes ----------
Call Stack:
d:\users\kageyu\documents\visual studio 2013\projects\heapleak\heapleak\main.cpp (10): HeapLeak.exe!main + 0x13 bytes
f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (626): HeapLeak.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (466): HeapLeak.exe!mainCRTStartup
0x748B7C04 (File and line number not available): KERNEL32.DLL!BaseThreadInitThunk + 0x24 bytes
0x76FFB90F (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x8F bytes
0x76FFB8DA (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x5A bytes
Data:
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........
0D F0 AD BA 0D F0 AD BA 0D F0 AD BA 0D F0 AD BA ........ ........


Visual Leak Detector detected 1 memory leak in heap 0x010D0000
```

However, according to the [HeapDestroy](http://msdn.microsoft.com/ja-jp/library/windows/desktop/aa366700%28v=vs.85%29.aspx) document:
> Processes can call HeapDestroy without first calling the HeapFree function to free memory allocated from the heap.


So I'm wondering if it is an actual memory leak or a false positive.

Comments: You can use the VLD_OPT_SKIP_HEAPFREE_LEAKS option to prevent those false leak reports, here's an example usage: ``` #include <windows.h> #include <vld.h> int main() { HANDLE hHeap = HeapCreate(0, 0, 0); LPVOID pHeap = HeapAlloc(hHeap, 0, 1024); // Skip HeapFree intentionally. // Fetch the current VLD options state VLD_UINT options = VLDGetOptions(); // Turn on the VLD_OPT_SKIP_HEAPFREE_LEAKS option VLDSetOptions((options | VLD_OPT_SKIP_HEAPFREE_LEAKS), 0, 0); HeapDestroy(hHeap); // returns TRUE // Restore VLD options VLDSetOptions((options), 256, 0); return 0; } ```

Viewing all articles
Browse latest Browse all 704

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>