开发者

Memory leak detection for mixed mode projects: managed, unmanaged and native

开发者 https://www.devze.com 2023-04-13 07:29 出处:网络
I have a Visual Studio 2010 solution that contains C# (managed), C++/CLI (unmanaged) and pure C++ (native) projects. I would like to perform memory leak detection across all 3 projects or at least aro

I have a Visual Studio 2010 solution that contains C# (managed), C++/CLI (unmanaged) and pure C++ (native) projects. I would like to perform memory leak detection across all 3 projects or at least around the native code:

  • The C# project references the unmanaged dll (I have access commonly available .NET Memory Profiling tools, so it's not really a problem to run memory profiling on it).
  • The C++/CLI is a very thin wrapper around the native C++ library, so I don't really need to profile it (not that worried about it).
  • The C++ native code is the one that's the most difficult to profile.

I've tried using Intel Inspector XE 2011, but it's simply too slow... doing a simple thing like merely initializing my system and takes so long that I haven't even seen it complete yet. When I run my system without IXE 2011, it takes me no more than 10-15 seconds to initialize my syste开发者_C百科m, while with IXE we've let it run for hours and it doesn't get past initialization. I've attempted to exclude certain libraries from being profiled, but it had absolutely no effect.

I've tried using the Visual Leak Detector, but after completing the run it said that it couldn't find any memory leaks. I was suspicious about that result so I intentionally placed a piece of code in a frequently run function to ensure that there is a memory leak:

int* memoryLeak = new int;

I ran with VLD again, but it spit out the same message. I'm considering overriding the new/delete operators or even just the malloc/free, but I wanted to make sure that I've exhausted all other options before I delve into doing that.

What can I do to profile the memory usage of my native C++ library with Visual Studio 2010? Are there any other tools or techniques that might work (even if they don't integrate with VS2010)?


In .NET even if you use managed objects there may be something that never get disposed (check some examples here: Memory Leak in C#).

About the native part, you may use two different approaches:

  • use a different memory profiler software, many listed here: Is there a good Valgrind substitute for Windows?

  • change your sources to use debug malloc/new and to print where in the code the allocations are done: http://www.flipcode.com/archives/Detecting_Memory_Leaks.shtml


Make unit tests which will test the units for memory leaks: http://msdn.microsoft.com/en-us/library/x98tx3cf.aspx

Get the first memory state before allocating your object and the second after release it. Compare your memory states after.

Also you can try to use other profilers like valgrind, devpartner.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号