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.
我有一个托管的DLL调用本机库。此本机库通常返回IntPtrs。这些可以传递给本机库中的其他方法来执行操作,或者告诉库释放与IntPtr关联的实例。但是只有一些实例需要以这种方式释放,其他实例则由库管理。问题是文档并不总是清楚哪些实例必须被释放,哪些实例不能被释放。
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?
我想知道的是,如果有一种方法可以判断我的代码是否保留了对必须释放的任何指针的引用,那么是否导致内存泄漏?
3 个解决方案
#1
1
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.
最简单的方法可能是使用内存分析器。一个快速的谷歌出现了一个链接到MemProfiler。我已经使用过一次(作为试用版),我能够找到我没有正确处理DirectoryEntries的地方。我确定还有其他人,包括RedGate的这个。
#2
1
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).
我使用的是WinDbg(可在此处获得)。它的命令行驱动,但提供了很多好的报告,包括堆栈信息,对象数量和采取的大小(这可以帮助指向未被处置的项目)。
There's also the Debug Diagnostic tool which has specific reporting for Memory and Handle Leaks. Its here
还有Debug Diagnostic工具,它具有Memory和Handle Leaks的特定报告。它在这里
#3
1
You may wish to consider using SafeHandles to wrap the handles returned from the Native code. It provides some additional value over an IntPtr.
您可能希望考虑使用SafeHandles来包装从Native代码返回的句柄。它为IntPtr提供了一些额外的价值。
#1
1
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.
最简单的方法可能是使用内存分析器。一个快速的谷歌出现了一个链接到MemProfiler。我已经使用过一次(作为试用版),我能够找到我没有正确处理DirectoryEntries的地方。我确定还有其他人,包括RedGate的这个。
#2
1
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).
我使用的是WinDbg(可在此处获得)。它的命令行驱动,但提供了很多好的报告,包括堆栈信息,对象数量和采取的大小(这可以帮助指向未被处置的项目)。
There's also the Debug Diagnostic tool which has specific reporting for Memory and Handle Leaks. Its here
还有Debug Diagnostic工具,它具有Memory和Handle Leaks的特定报告。它在这里
#3
1
You may wish to consider using SafeHandles to wrap the handles returned from the Native code. It provides some additional value over an IntPtr.
您可能希望考虑使用SafeHandles来包装从Native代码返回的句柄。它为IntPtr提供了一些额外的价值。