views:

215

answers:

3

I have a managed dll that calls into a native library. This native library generally returns IntPtrs. These can be passed in to other methods in the native library to do things, or to tell the library to free the instance associated with the IntPtr. But only some of the instances need to freed in this way, others are managed by the library. The problem is that the documentation is not always clear about which instances must be freed and which must not.

What I want to know is if there is a way that I can tell if my code has kept references to any of the pointers which must be freed, and so is causing memory to leak?

+1  A: 

The easiest way is probably to use a memory profiler. A quick google turned up a link to MemProfiler. I've used this once (as a trial) and I was able to find places where I wasn't properly disposing of DirectoryEntries. I'm sure there are others, including this one by RedGate.

tvanfosson
+1  A: 

I use WinDbg (its available here). Its command-line driven but provides lots of good reports include stack information, numbers of objects and size taken (this can help point to items that are not being disposed).

There's also the Debug Diagnostic tool which has specific reporting for Memory and Handle Leaks. Its here

ScottCher
A: 

You may wish to consider using SafeHandles to wrap the handles returned from the Native code. It provides some additional value over an IntPtr.

dpp