Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 system, 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)?

like image 819
Kiril Avatar asked Oct 14 '11 20:10

Kiril


People also ask

Which function can be used to avoid a memory leak?

Make sure to call free() when you use malloc() or calloc() . Don't reassign pointer which points to allocated memory location without free() ing it first i.e. don't lose the reference.

What is memory leak What are different types and how do you avoid them?

Memory leak occurs when programmers create a memory in heap and forget to delete it. The consequences of memory leak is that it reduces the performance of the computer by reducing the amount of available memory.

What are the types of memory leaks?

There are two types of memory leaks: apparent and subtle. An apparent memory leak is a chunk of heap memory that's never referred from active memory, a subtle leak is memory that is still referred to but shouldn't be, i.e. a hash or dynamic array holds the references.


1 Answers

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

like image 187
Sga Avatar answered Sep 28 '22 09:09

Sga