Using Memory Leak Detector to detect Memory Leaks in 4 steps
Step 1. Include allocator.h from your source
To make your program use checking version of operator new you need to include allocator.h from your source. Also you need to define symbol CHECKED_ALLOCATION prior to doing this. Example code:
#define CHECKED_ALLOCATION
#include "allocator.h"
int main() {
char* niz = new char[50];
char* blahah = new char;
char* z = 0;
delete z;
delete blahah;
return 0;
}
Best place for including allocator.h is after including system and STL files (since we all hope for them not to have any memory leaks, and some of them may contain weird macros which MLD's macros can mess up :-) )
Step 2. Include allocator.cpp in your project
This is very much environment dependant so it won't be explained here, but you shouldn't forget to do this in order linker to find definitions of checked new and delete operators. Please note that it may be good idea to enable maximal compiler's optimizations on this file (allocator.cpp) in order to get maximal performance while running your program.
Step 3. Compile and build your program
Again very environment dependant, and I am sure you know how to do that.
Step 4. Run your program
After your program finish its running on standard error (file descriptor 2) will be written locations in your source where is allocated memory which wasn't released until end of execution. Please note that every location will be written only once, so if you have fragment like this (with line numbers):
22: for (int i = 0; i < 10; i++) {
23: char* leak = new char[127 + i];
24: sprintf(leak, "%d", i);
25: }
You won't get line number 23 written 10 times but just once. Of course if you don't like default behaviour of library to display memory leaks on stderr you can change that in library source.
Don't forget: After resolving all memory leaks don't forget to remove MLD's code (allocator.cpp and allocator.h) from your project. You probably don't want your users to see report about memory leaks if you missed to correct some of them.
