2

I got the Problem, that my application has an infinite growing Memory Leak, which is not detected. What I do very simplified, is to create an object, run a method on it and then delete the object. Each time I do this, the memory usage in the TaskManager grows by around 50-100MB. This exhausts my whole memory after some runs. I do this by multithreading, but there are no static variables, so there is no collision between the different objects in my threads. They only use static methods of other objects, that don't modify any other memory than passed in the parameters - so it's thread-safe. What I tried to find out the reason:

  • Using crtdbg.h (CRT-Memeory-Leak-Detection), but there are only leaks which exist since the start of my application - they will get deleted at shutdown and they are not that big.
  • I was looking for the virtual destructors in all the objects that I inherit from, but they are all OK

What else can I try to find out where my application leaks? I can't find any leaks in the HEAP and I don't know any other reasons than the destructor problem that can cause Leaks in the STACK (by this I mean that an object doesn't destroy a local std::string objects which has allocated space in heap). I don't know if there are other reasons for "STACK-Leaks", but I know that in the parts of my method, where the memory grows the most, there are no HEAP allocation.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
DenisD
  • 21
  • 3
  • 1
    Let's see code for object creation and what you do with the object or pointer to the object – mmmmmm Jul 17 '12 at 14:42
  • The Obejct is very large, so i can't post the whole code - it's allocated by new and deallocated using delete - what do u want to see exactly - each new has a delete and each new[] has a delete[] ... – DenisD Jul 17 '12 at 14:53
  • If you only do that why not create the object on the stack - so no need for new and delete – mmmmmm Jul 17 '12 at 14:56
  • @Mark - probably because the OP has tagged 'multithreading' Where objects are communicated between threads, it can be extremely dodgy creating objects on stacks - the curse of RAII may well blow it away before it is handled by other threads. – Martin James Jul 17 '12 at 15:02
  • Be very wary of trying to detect leaks with Task Manager. 'This exausts my whole memory after some runs.' - what happens, you get an exception? Try minimizing your app and restoring it again - see what happens. – Martin James Jul 17 '12 at 15:06
  • @MartinJames - In that case how are they passed perhaps there is something retaining it - ie we need to know more details – mmmmmm Jul 17 '12 at 15:06
  • " but I know that in the parts of my method, where the memory grows the most, there are no HEAP allocation" => What if the stack objects internally allocate memory from heap and not releasing? – PermanentGuest Jul 17 '12 at 15:11
  • @PermanentGuest There are no stack-objects allocating memory which i wrote - only some std::strings/std::map, but i think they should deallocate when they are released from the stack by exiting my method/destroying the object! I do not allocate any heap memory by myself - in the mothode or the objects used in there - where there is no delete. Thats why i'm so confused! – DenisD Jul 17 '12 at 15:33
  • Since your minimal code is creation of a single object and this leaks appreciable memory, you could easily divide and conquer the problem. Comment half of the code that is executed and check if the problem is reproducible. If yes, the problem is in the remaining code, otherwise the problem is in the other half. Repeat this till the problem is localized. But, remember to comment out pairs of initialization/clean-up together. – PermanentGuest Jul 17 '12 at 15:37

2 Answers2

1

You probably want to use a nicer, more robust leak detector. You may also need to use a leak detector that can output a heap report at different times while your program is running. Finally, you should consider that your problem might be due to heap fragmentation rather than just a leak.

You can try Visual Leak Detector which is free from Google.

This question contains a list of other memory check products, from the basic to the quite advanced/expensive. CRTDBG is the lowest-common-denominator solution; I've had good luck with BoundsChecker, although it is not free.

Community
  • 1
  • 1
antlersoft
  • 14,636
  • 4
  • 35
  • 55
  • But what is the difference to the CRT Leak Detector - so what advantage does it have? Does the CRT not detect everything or why should i use Virtual Leak Detector? – DenisD Jul 17 '12 at 14:55
  • @DenisD: From the Google page: `The main difference ... is that Visual Leak Detector shows you the complete callstack used for memory allocation has led to the leak.` – tinman Jul 17 '12 at 15:44
1

Not sure how you have used CRTDBG library but it provides lots of goodies:

http://msdn.microsoft.com/en-us/library/x98tx3cf.aspx

You can use _CrtMemCheckpoint in divide and conquer manner. It allows you to measure difference in memory use between two points in your code. With multithreading this can be difficult.

Another is _CrtDumpMemoryLeaks (which i suppose is executed anyway on app end) with _CRTDBG_MAP_ALLOC enabled, this should show exact location of memory allocations.

Another hint is that maybe you have your CRTDBG overconfigured, with lots of small allocations it can create huge internal memory structures.

Try switching off parts of your code, and check if problem persists.

If you build your app on daily basis, try running previous versions to spot where problem appeared, then compare changes in source code repository.

...

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • I used _CrtDumpMemoryLeaks when my "main" object i run in a thread is destroyed and after the thread exited. It did not show anything left by this object. – DenisD Jul 17 '12 at 15:44