I just learned how to look for memory leaks from this forum, and found my code has one. I eliminated all code not relevant to the memory leak. The code compiles and links (VC express), you can just create a console application to test it!
Set two break points at lines 12 of test.cpp: where is the memory leak?
Simplifying the code any further yields no memory leak report.
#include "stdafx.h"
#include <iostream>
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
void onExit( void )
{
_CrtDumpMemoryLeaks();
}
int _tmain(int argc, _TCHAR* argv[])
{
// Register the function that will be called
// when the program exits
atexit(onExit);
// Code to test....
int* pInt = newint(2);
// now for a memory leak
pInt = newint(3);
delete pInt;
return 0;
}
Got it! Thanks, it unfortunately means _CrtDumpMemoryLeaks() has less of a use for me... A question naturally comes.
I have a lot of global objects in two different files. So, I need to call _CrtDumpMemoryLeaks() at the destructor of the class of the first global object created (since it will be the last one destroyed, right?). Now which of the two files takes priority?
The _CrtDumpMemoryLeaks function determines whether a memory leak has occurred since the start of program execution. When you program finishes the net result of allocated and deallocated memory should be zero. If it is not _CrtDumpMemoryLeaks will just list the un-deallocated data. You can sometimes work out what has not been deallocated from the data, but from the sound of your posts this will likely be just lots of numbers.
The thing you will need to do is run your code with a small data-set and see if that produces a memory leak, increasing the size until it does. Then look at your code to see what is happening with that size data-set, the need to grow your list...
... or just take a good look at the places you call new and delete
And you get this result: Detected memory leaks!
Dumping objects ->
{158} normal block at 0x003498F0, 32 bytes long.
Data: <Test const strin> 54 65 73 74 20 63 6F 6E 73 74 20 73 74 72 69 6E
Object dump complete.
Apparently with that code _CrtDumpMemoryLeaks() is getting called before all object deallocation has been completed by the application. If you instead put this code
[Edit:] besides the below measures I took for a memory leak I found where my actual memory leak. I will post a topic about it in the lounge since I feel like shooting the breeze about c++ :)
In fact I now adopted the "Garbage collector" trick in order not to put too much thought into it.
in code form:
1 2 3 4 5 6 7 8 9 10
//file: polyhedra.h
class CombinatorialChamber
{
public:
CombinatorialChamber();
};
class CombinatorialChamberPointers:public ListObjectPointers<CombinatorialChamber>
{
public:
};