1

I've been trying for a few hours now to get it working. So far it does find memory leaks but it finds a ton and I'm not sure if that's realistic. Also I'd like to see the file and line number (I know it's possible but I can't get it to work) so that I can actually solve the memory leaks.

I have added the code to dump memory leaks in a method that is frequently called (about 60 times per second normally), I'm not sure if it's ok or not but since I don't really have a "main" function it's hard to decide where to put the code.

Here is what I have added :

// This part is in the includes part of the file
#define _CRTDBG_MAPALLOC
#define _CRTDBG_MAP_ALLOC_NEW
#include <stdlib.h>
#include <crtdbg.h>

#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

// This part is in the method
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
_CrtDumpMemoryLeaks();

Thanks for the help!

3
  • try deleaker or similar debugger - it help to control the memory leaks and user objects... it will indicate the line that located a bug.
    – John Smith
    Mar 15, 2012 at 16:56
  • The thing is my file is a .jar and deleaker will only accept a .exe. Do you know any similar tool that would work with a Java program that calls C++ code through JNI? Thanks. Mar 15, 2012 at 23:22
  • Every allocation is a leak when you call _CrtDumpMemoryLeaks() all the time. Consider using DllMain() for a good trigger. Mar 16, 2012 at 12:36

2 Answers 2

1

Try

Visual Leak Detector

http://www.codeproject.com/Articles/9815/Visual-Leak-Detector-Enhanced-Memory-Leak-Detectio

I had very good results with it.

0

With a crt you have the possibility to compare the memory state before perform an action and after that. In order to achieve this just use the _CrtMemDifference in your unit tests. The concept is to write the unit-tests which will call a different parts of your code and will get the state of the application memory before by using the _CrtMemCheckpoint and after the call. So after that you have to compare two checkpoints with the _CrtMemDifference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.