I'm debugging a program doing a bundle block adjustment that someone else wrote. I used the memory leak detector macros of Visual Studio (2008) i.e.
# define _CRTDBG_MAP_ALLOC
# include <stdlib.h>
# include <crtdbg.h>
main( int argc, char* argv )
using namespace std;
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
unsigned allocNumber = 6015;
if( allocNumber > 0 )
} // end main()
I also tried Visual Leak Detector, as suggested in another thread. Both found 2 memory leaks at the end of the program. But if I use the function _CrtDumpMemoryLeaks() just before exiting (i.e. return EXIT_SUCCESS, I have a whole bunch of "detected" memory leaks.
Even with the VS macros, I can't have the file name and the line number of the faulty call. With VLD, I have more info. But what about the other "detected" leaks? Are they real leaks since the memory seems to be freed at the exit of the program and they are not detected by the two tools?
One concern I have is that for a small sets of inputs, the program runs well and fast (at least, except for the two leaks). For large inputs, the running time is unpredictable. I suppose that at each iteration of the bundle adjustment new objects are created to store the new computed values of the variables and that the old objects are not freed. The memory gets saturated and "something" (the OS?) is dumping objects during run time, thus the different running times recorded (the time for "something" to free the memory).
I think I have found the faulty class, Data. It is nested in another class, template< typename T > class Matrix. T is double by default. Here is the code of Data (I have added some optional commentaries).
In each contructor, the attribute m_deletable is set correctly (there is one Matrix:ata that needs to be kept in memory from beginning to end, hence m_deletable(false)). What I am not sure about is the destructor: it does not seem to be called when needed (assuming again the "detected" leaks are real). I did not find any other class being the child of the class Data, so it looks strange that the destructor is virtual. Is there an other way to write this destructor?
If needed, I can post the code for the ReferenceCountedObject class inherited by Data.
I'm using Visual Studio 2008 on Windows XP pro x64. I have four builds: Win32-debug, Win32-release, x64-debug and x64-release. The same two leaks (and the other "detected" ones) appear in every build.
Thanks for your help! Any comment is welcome!
Last edited by Marc G; March 12th, 2010 at 02:31 AM.
Reason: Added code tags