Fern   www.davinpearson.com
New Zealanders making it harder to hate computers

    Main Menu     Research Projects      Photo Album      Curriculum Vitae   Greatest Artists  
  Email Address   Computer Games       Web Design       Political Activism      Other Links     
Debugging Macros String Class I Linked List System I Java for C Programmers Naming Convention
String Class II How I use m4 Strings III Symmetrical I/O Linked List System II
Run-Time Type Info Virtual Methods An Array System Science & Religion Submodes
Nested Packages Memory Leaks Garbage Collection Internet & Poverty What is Knowledge?
Limits of Evolution Emacs Additions Function Plotter


A Memory Leak Detection System for C++

Abstract

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.

1. The system

Here is a class for implementing DMM:
#ifndef ALREADY_INCLUDED_DMM_CLASS_HH
#define ALREADY_INCLUDED_DMM_CLASS_HH

class Dmm
{
public:
   virtual ~Dmm() { } /// ensures VTABLE* is the first element

public:
#ifdef DMM_ONLINE
   enum_debug_code debug_code;
#endif /* DMM_ONLINE */

   Dmm(enum_debug_code debug_code)
   {
      (void)debug_code;
#ifdef DMM_ONLINE
      this->debug_code = debug_code;
#endif /* DMM_ONLINE */
   }

   ///
   /// NOTE: disables passing and returning by value
   ///
private:
   Dmm(const Dmm&);
   Dmm& operator = (const Dmm&);
};

#endif /* ALREADY_INCLUDED_DMM_CLASS_HH */

Click here to view the definition of the enum enum_debug_code. Every class that implements DMM should inherit from the above class like so:
class Foo : public Dmm
{
public:
   Foo() : Dmm(DCODE_FOO)
   {
   }
};

Here is the header file for DMM: dmm.hh Here is the footer file for DMM: dmm.cc Here is a sample main function:
#include "../../2003/noio/io.hh"
#include "foo.hh"

int main()
{
#ifdef ALLEGRO_ONLINE
   allegro_init();
#endif /* ALLEGRO_ONLINE */

   cout << "**** BEGIN t-dmm.exe\n";

   Foo* f = new Foo();
   (void)f;

#ifdef DMM_ONLINE
   dmm_debug_print();
#endif /* DMM_ONLINE */

   cout << "**** END   t-dmm.exe\n";

   return EXIT_SUCCESS;
}
#ifdef ALLEGRO_ONLINE
END_OF_MAIN();
#endif /* ALLEGRO_ONLINE */



When you run this program, it will print a list of all unfreed memory for debugging purposes.

Back to Research Projects

| Main Menu | Research Projects | Photo Album | Curriculum Vitae | Greatest Artists |
| Email Address | Computer Games | Web Design | Political Activism | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked List System II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter |

Please report any broken links to the webperson
Last modified: Thu Jun 14 22:17:00 NZST 2007
© Copyright 1999-2007 Davin Pearson.



Hosted by www.Geocities.ws

1