Main Page   Reference Manual   Namespace List   Compound List   Namespace Members   Compound Members   File Members  

Classes | Functions
Memory Allocation Markers: Memory Leak Checking
Collaboration diagram for Memory Allocation Markers: Memory Leak Checking:


class  libcwd::marker_ct
 A memory allocation marker. More...


void libcwd::move_outside (marker_ct *marker, void const *void_ptr)
 Move memory allocation pointed to by ptr outside marker.

Detailed Description

Detailed Description

Libcwd does a weak attempt to support debugging of memory leaks.  I hope to greatly improve this in the future.It is possible to mark allocations that are done till that moment, and then later check for memory leaks by expecting no other memory allocations than those that already existed before the mark.  This is done by creating a marker_ct object.  The check for memory leaks is done when the marker is removed again.  This can be done recursively.A marker is created by passing it a description:
libcwd::marker_ct* marker = new libcwd::marker_ct("Description of the marker");
Any allocation done after the creation of this marker will be reported as a leak at the moment the marker is deleted.Markers are clearly visible in the Allocated memory Overview.  They are labeled MARKER (see also the allocator type table).  All memory that is allocated after a marker, is displayed indented and below that marker in the Allocated memory Overview.Finally, it is possible to move specific memory blocks outside markers, so they will not cause a memory leak detection.  This is done with the function
namespace libcwd {
void move_outside(marker_ct* marker, void const* ptr);
which would move the memory allocation pointed to by ptr outside the test region of marker.A complete example program with output is given here:
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#include <libcwd/sys.h>
#include <libcwd/debug.h>
// A dummy class
class A {
int i;
int j;
char k;
int main(int argc, char* argv[])
// Don't show allocations that are allocated before main()
// Select channels
ForAllDebugChannels( if (debugChannel.is_on()) );
Debug( dc::notice.on() );
Debug( dc::malloc.on() );
Debug( dc::warning.on() );
// Debug( dc::bfd.on() );
// Write debug output to cout
// Turn debug object on
Debug( libcw_do.on() );
// Allocate new object
A* a1 = new A;
AllocTag(a1, "First created");
// Create marker
libcwd::marker_ct* marker = new libcwd::marker_ct("A test marker");
// Allocate more objects
A* a2 = new A[10];
AllocTag(a2, "Created after the marker");
int* p = new int[30];
AllocTag(p, "Created after the marker");
// Show Memory Allocation Overview
Debug( list_allocations_on(libcw_do) );
Dout(dc::notice, "Moving the int array outside of the marker...");
Debug( move_outside(marker, p) );
// Show Memory Allocation Overview
Debug( list_allocations_on(libcw_do) );
// Delete the marker
delete marker;
Dout(dc::notice, "Finished successfully.");
DoutFatal(dc::fatal, "Please reconfigure libcwd with --enable-alloc.");
return 0;
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.