C++ Memory Management System

OBSELENCE NOTE: This was written several years ago, so may be outdated (ie. may have broken links — if so, let me know and I’ll update).  Just because it’s old doesn’t mean that it’s not worth sharing!

Summary:

Here’s a handy (and free) snippet of code that overloads the ‘new’, ‘new[]‘ and ‘delete’ operators.  The new overloaded functions keep track of memory allocations and de-allocations, therefore allowing you to summarize memory that wasn’t de-allocated.

Note that the code that you’ll see here is derived from some of the code shown in the references.  I didn’t find a complete all-in-one package that I really liked and had the features I wanted (like dumping a summary to a log file, being able to tell it to echo allocations to stdout, etc).  This is not all coded from hand, therefore much of the credit goes to the people who made the pages in the references section!

Instructions:

Instructions are found in CleanMem.h.  For reference, here’s the instructions in CleanMem.h:

/////////
// –Instructions for use:–
// 1. define any desired options
// 2. include this file in your C++ file
// 3. if you’d like to see a summary of memory leaks, run the function
//     DumpUnfreed(); at the point where you’d like to see memory leaks (at
//     the end of a program right before you exit / return is a good spot).
//
//
// POSSIBLE DEFINABLE OPTIONS:
// —————————
// __CLEAN_MEM_LOCAL_ECHO
//
// Defined:     echo memory allocations / deallocations to stdout (console app)
// Undefined: do not echo memory allocations / deallocations to stdout
//
//
// __CLEAN_MEM_WRITE_TO_LOG
//
// Defined:     Write memory allocs/deallocs to log file…
// Undefined: Don’t write memory allocs/deallocs to log file
//
//
// __CLEAN_MEM_LOG_FILE
//
// Defined:     Filename of log file that output is written to
// Undefined: defaults to “CleanMem.log”
//
//
// –End of instructions –
/////////

For a sample program that uses CleanMem.h, download CleanMem.cpp.  This program shows how to use CleanMem.h and a sample of the output you’ll get with it.

References (what this code is derived from):

Files:

CleanMem.h (The C++ Memory Management System header file)

CleanMem.cpp (Example program)

Related posts:

  1. Windows 2000/2003 Directory Services Audit Logging Management Utility Summary: There’s a way to get very detailed event logging...
  2. Interaction of stdout buffering using standard stdio.h functions when using pthreads I’ve been working on a project recently that stretched my...
  3. NT Authentication DLL OBSELENCE NOTE: This was written well over a year ago,...

Related posts brought to you by Yet Another Related Posts Plugin.

Tags: , , , , , , , , ,

Leave a Reply