This article presents a memory leak detection system
called D.M.M. (Debug Memory Management) for finding
memory leaks in C++ programs when the symbol NDEBUG is not set.
This article has been superseded by
a later article.
1. The system
Here is a class for implementing D.M.M.:
#ifndef ALREADY_INCLUDED_DMM_CLASS_HH
#defineALREADY_INCLUDED_DMM_CLASS_HHclassDmm
{
public:
virtual ~Dmm() { } /// ensures VTABLE* is the first element
public:
#ifdefDMM_ONLINEenum_debug_codedebug_code;
#endif/* DMM_ONLINE */Dmm(enum_debug_codedebug_code)
{
(void)debug_code;
#ifdefDMM_ONLINEthis->debug_code = debug_code;
#endif/* DMM_ONLINE */
}
/// /// NOTE: disables passing and returning by value ///private:
Dmm(constDmm&);
Dmm& operator= (constDmm&);
};
#endif/* ALREADY_INCLUDED_DMM_CLASS_HH */
Click here to view the
definition of the enumenum_debug_code. Every class that
implements D.M.M. should inherit from the above class like so:
class Foo : public Dmm
{
public:
Foo() : Dmm(DCODE_FOO)
{
}
};
Here is the header file for D.M.M.: dmm.hh Here is the footer
file for D.M.M.: dmm.cc Here is a sample main function:
#include"../../2003/noio/io.hh"#include"foo.hh"intmain()
{
#ifdefALLEGRO_ONLINEallegro_init();
#endif/* ALLEGRO_ONLINE */cout << "**** BEGIN t-dmm.exe\n";
Foo* f = newFoo();
(void)f;
#ifdefDMM_ONLINE
dmm_debug_print();
#endif/* DMM_ONLINE */cout << "**** END t-dmm.exe\n";
returnEXIT_SUCCESS;
}
#ifdefALLEGRO_ONLINEEND_OF_MAIN();
#endif/* ALLEGRO_ONLINE */
When you run this program, it will print a list of all unfreed memory
for debugging purposes.