I have a .NET application that interops with native dll with following layers.
a) C++ API GetNames() returns a list of wchar_t instances
b) C++/CLI API GetNames2() code calls into C++ API, marshall the list of wchar into .NET string, return this .NET string list
c) managed code C# calling interop layer GetNames2() API and display list of string
I am calling CoTaskMemFree API in interop layer to free natives wchar strings. However I am just wondering how to see memory leak caused by a missing CoTaskMemFree call. So I commented out call to CoTaskMemFree. In this case my understanding is that if I attach windbg and run !address -summary command, any allocations done on the native side (that haven’t been freed) should be shown under RegionUsageHeap but it does not seem to report it. Any ideas how can I see native memory usage (in context of managed code) within windbg?
The RegionUsageHeap is the total used for all native heaps, this value is incremented in quite large chunks, and there must be a quite huge memory leak to be observed in this variable.
You can use the !heap command. The CoTaskMemAlloc uses default per-process heap, which usually is the first handle in the output from !heap –s , or look after “ProcessHeap” in the !peb output.
When you have this handle use !heap –stat –h to see how many blocks there is allocated.
You may also dump the entire heap, check the windbg help for the !heap command. Sample: