Thiago R. Adams website

Home Blog Code-Blog Twitter Downloads Links / Books About

Websites

gc - Simple class to detect memory leaks

Use this small class to detected memory leaks in your application. To use it just derive your class from the gc class. If any object is still alive in the end of your application, the method OnMemoryLeak will be called.

Generally I use an assert(false) to customize the OnMemoryLeak method.


#ifndef __GC_H__
#define __GC_H__

#ifdef _DEBUG

#include <vector>
#include <algorithm>
#include <functional>
#include <typeinfo>

class gc
{
    struct gcList : public std::vector<gc*>
    {
        ~gcList()
        {
            std::for_each(begin(), end(), std::mem_fun(&OnMemoryLeak));
        }
    };

    static gcList& GCList()
    {
        static gcList vec;
        return vec;
    }

  void OnMemoryLeak(void)
  {
     const char *leak_in = typeid(*this).name();
     //do something here :)
  }

protected:
  gc()
  {
    GCList().push_back(this);
  }

  virtual ~gc() {
      std::vector<gc*>::iterator it = std::find(GCList().begin(), GCList().end(), this);
      if (it != GCList().end())
         GCList().erase(it);
  }
};

#else

// it does nothing in release
class gc {};

#endif


#endif //__GC_H__
}}}

Want to see more? Go to the CodeBlog section.

About the author: I am Thiago Adams. I work as a professional C++ software engineer. I have created this website to share ideas and source code with other people with similar interests.
I would like to hear from you comments, critics, questions and suggestions about this topic or any other part of this website. Email: thiago.adams at gmail dot com