Convenient tool for simple memory checking

Article ID: 2059
Last updated: 11 Apr, 2018
Article ID: 2059
Last updated: 11 Apr, 2018
Revision: 3
Views: 1460
Posted: 15 Mar, 2013
by Meurgues A.
Updated: 11 Apr, 2018
by Rehme L.

Memory issues are not easy to track and we have not always a good memory tracker available. Here is a small set of classes that can help to identify memory issues.


We redefine new, new[], delete and delete[] operators. All these operators will then ask memory to the Allocator singleton where allocations and deletions will then be able to be traced.

It means that only memory allocations done through new and new[] will be traced. The memory allocated by malloc/calloc cannot be traced by this way.

Classes Description


This is an allocator that can be passed as parameter to standard containers (list, maps,...) for allocating memory when adding an element.

The standard allocator uses operator new() to allocate memory. Our memory monitor must store information about the memory used without being traced itself. Otherwise, it would monitor its own memory which is not what we want. Moreover, it would enter into an infinite recursive call.


This is a singleton class which will actually allocate/deallocate the memory through malloc and free funct. Operators new and new[] will call Allocator::Malloc(). Operators delete and delete[] will call Allocator::Free().

It is possible to add a listener for being notified of each allocation and deallocation. The listener must be a subclass of the abstract class Allocator::MemoryMonitorListener. The function for adding and removing listeners are:

void Allocator::AddListener(MemoryMonitorListener*);

void Allocator::RemoveListener(MemoryMonitorListener*);


This is a exemple of MemoryMonitorListener. It stores a description of each piece of memory allocated into a map and remove it from the map when deallocated. It is then able to detect if a deallocation deallocate a memory chunk that had not been allocated.

void AllocatedMemoryListener::dumpChunks() const;

Write on standard output (printf) the list of all allocated memory chunks.

void AllocatedMemoryListener::dumpResults() const;

Write a short resumé of the memory allocation:

  - total allocated

  - total freed

  - delta allocated (memory allocated not yet deallocated)

void AllocatedMemoryListener::setSilent(bool silent);

if silent==false, the memory listener will write a trace when:

  - a memory chunk is deallocated without having allocated

  - a memory chunk is allocated twice

This is set to true by default.

void AllocatedMemoryListener::reset();

Reset all the registered information.


This class is a RAII class to ease memory report for a piece of code. It installs a AllocatedMemoryListener on creation and prints a report (AllocatedMemoryListener::dumpResults()) on deletion.

The constructor takes a name that is printed at the head of the report.

void AllocationEvaluator::print(const char* comment = 0) const;

Allow to print a memory report at any moment before the last report printed during destruction.

You can then optionally add a comment which will be added to the header after the name.

void AllocationEvaluator::reset();

Reset all registered information.


Here is a typical use of this API:

#include "allocator.h"
#include <cstdio>
#include <iostream>
int main() {
RWSupport::AllocationEvaluator eval("10 doubles");
for (int i=0; i<10; ++i) {
double* d = new double;
delete d;
d = new double;
} /* here, eval is deleted, and a report is then printed on all what happened
     since its creation at the begining of the block */
char c; std::cin >> c; // avoid the shell to be close if launched from Visual Studio
return 0;

And here is the produced output:

10 doubles:
total allocated:     160
total freed:          80
delta allocated:      80

This article was:   Helpful | Not helpful
Report an issue
Article ID: 2059
Last updated: 11 Apr, 2018
Revision: 3
Views: 1460
Posted: 15 Mar, 2013 by Meurgues A.
Updated: 11 Apr, 2018 by Rehme L.
Attached files

Also listed in

Others in this category