PRODUCT : Borland C++ NUMBER : 1756 VERSION : 3.1 OS : DOS/WIN DATE : January 7, 1994 PAGE : 1/3 TITLE : How to find out if you're leaking memory This TI contains a function called leak_chk() that illustrates one method of detecting memory leaks. The example code calls leak_chk() in the beginning to get the starting block count and stores it. The the example creates an intentional memory leak, by reusing a pointer already in use. All through the code leak_chk() is called to point out the lost chunk of memory. In a your program you would call leak_chk() in the beginning and again at the end. Then subtract the starting value from the ending value and the result should be zero, no leaks. leak_chk() is very basic, but you could expand on it to track all your allocations and freeing of memory. Then at any point you could check for leaks. Many users try to use coreleft() as a tool for finding memory leaks. I have sprinkled calls to coreleft() through out the code to show that it does not help in finding leaks. coreleft() only reports the available memory at the top of the heap. Check the library reference on coreleft() for details on how it determines available heap based on memory model. As a final point be sure to read TI738 on Memory Corruption for tracking down memory related issues. With a function similar to leak_chk() and the debugging tips in TI738 finding some of those strange bugs in your program will be a lot easier. #include #include #include long leak_chk(); int main(void) { char *str; long start=leak_chk(); // allocate memory if ((str = (char *) malloc(10000)) == NULL) { cout << "Not enough memory to allocate buffer\n"; exit(1); // terminate program if out of memory } PRODUCT : Borland C++ NUMBER : 1756 VERSION : 3.1 OS : DOS/WIN DATE : January 7, 1994 PAGE : 2/3 TITLE : How to find out if you're leaking memory cout <<"Top of heap after first memory allocation: " << coreleft() << endl; // create intentional memory leak if ((str = (char *) malloc(10)) == NULL) { cout << "Not enough memory to allocate buffer\n"; exit(1); // terminate program if out of memory } cout <<"Top of heap after second memory allocation: " << coreleft() << endl; cout << "Memory leak, after second allocation: " << (leak_chk()-start) << endl; // free memory free(str); cout << "Memory leak, after freeing pointer: " << (leak_chk()-start) << endl; cout <<"Top of heap after freeing memory and before exit: " << coreleft() << endl; // Coreleft still looks reports lots of memory, but there is // large leak of unrecoverable memory. return 0; } long leak_chk() { // heapinfo is a predefined structure and holds the // information returned by heapwalk() struct heapinfo hi; int result; long size = 0; // Check if to see if heap is too corrupted to walk if((result = heapcheck()) != _HEAPOK ) { cout << "Heap not OK: " << result << endl; exit(1); } PRODUCT : Borland C++ NUMBER : 1756 VERSION : 3.1 OS : DOS/WIN DATE : January 7, 1994 PAGE : 3/3 TITLE : How to find out if you're leaking memory // For the first call to heapwalk, set the hi.ptr field // to null. hi.ptr = NULL; // Heapwalk walks through the heap node by node while(heapwalk(&hi) == _HEAPOK) { //in_use flag that's set if the block is currently in use. if(hi.in_use) // Holds the size of the block in bytes. size += hi.size; } return size; } DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.