MemoryLeakIndicator
Loading...
Searching...
No Matches
MemSpy::LeakMap Class Reference

Class to access forgotten objects in memory.
Inspired by Rainer Grimm and Mouaz Chamieh. More...

#include <MemoryLeakIndicator.h>

Classes

struct  MemoryItem
 Reference to the instantiated object with file name and line where it was instantiated. More...
 

Public Member Functions

void setLeakMapUseAllowed (bool bAllowed=true)
 
bool leakMapUseAllowed () const
 
unsigned long long numberOfProcessedObjects () const
 
std::size_t sizeOfObjectList () const
 
MemoryItemgetSpyObject (SeekMode eMode)
 
MemoryItemgetSpyObject (INT_PTR ipAddress)
 
void insertSpyObject (void *pNewObject, char const *chFileName, int iLine)
 
void removeSpyObject (void *pObjectToDelete)
 

Static Public Member Functions

static LeakMapcreate ()
 
static int destroy ()
 
static void destroyAbsolutely ()
 
static LeakMapgetGlobalLeakMapInstance ()
 

Protected Member Functions

 LeakMap ()
 
virtual ~LeakMap ()
 

Private Member Functions

 LeakMap (const LeakMap &)
 
LeakMapoperator= (const LeakMap &)
 

Private Attributes

CRITICAL_SECTION m_LocalCriticalSection
 
std::unordered_map< INT_PTR, MemoryItem * > m_ObjectList
 
std::unordered_map< INT_PTR, MemoryItem * >::iterator m_ObjectListIterator
 
unsigned long long m_ullInsertCounter
 

Static Private Attributes

static bool ms_bUseLeakMap
 
static int ms_iReferenceCounter
 

Detailed Description

Class to access forgotten objects in memory.
Inspired by Rainer Grimm and Mouaz Chamieh.

See also
https://www.grimm-jaud.de/index.php/blog/operator-new-und-delete-ueberladen und https://github.com/muazsh/MemoryLeakManager
Author
Helmut Jakoby

Constructor & Destructor Documentation

◆ LeakMap() [1/2]

MemSpy::LeakMap::LeakMap ( )
protected

The standard constructor is not available. You can get an instance via LeakMap::create().

◆ ~LeakMap()

virtual MemSpy::LeakMap::~LeakMap ( )
protectedvirtual

The destructor is not available. An LeakMap object may only be removed from memory with LeakMap::destroy().

◆ LeakMap() [2/2]

MemSpy::LeakMap::LeakMap ( const LeakMap & )
private

The copy constructor is not available.

Member Function Documentation

◆ create()

static LeakMap * MemSpy::LeakMap::create ( )
static

Instantiation function. Since LeakMap is a singleton class, this method instantiates it once and then only returns the pointer to the instance.

Returns
The only instance of this class.
Attention
The delivered instance must be removed from memory with LeakMap::destroy().

◆ destroy()

static int MemSpy::LeakMap::destroy ( )
static

If the calling instance is the last one with a reference to the only instance of LeakMap, this instance is removed from memory.

Returns
Return is the number of still-references to this instance.

◆ destroyAbsolutely()

static void MemSpy::LeakMap::destroyAbsolutely ( )
static

It removes the only instance of LeakMap from memory without querying ms_iReferenceCounter.

Attention
This should only be done at the end of the main.

◆ getGlobalLeakMapInstance()

static LeakMap * MemSpy::LeakMap::getGlobalLeakMapInstance ( )
static

Returns g_pVoidLeakMap cast to LeakMap if instantiated.

◆ getSpyObject() [1/2]

MemoryItem * MemSpy::LeakMap::getSpyObject ( INT_PTR ipAddress)

Returns a MemoryItem object from the list of MemoryItem objects which references the object with the passed address.

Parameters
[in]ipAddressThe address of the searched object cast on INT_PTR.
Returns
If found the MemoryItem object, otherwise NULL_PTR.
Attention
The calling instance must not remove the supplied object from memory.

◆ getSpyObject() [2/2]

MemoryItem * MemSpy::LeakMap::getSpyObject ( SeekMode eMode)

Returns a MemoryItem object from the list of MemoryItem objects related to eMode.

Parameters
[in]eModeThe Seekmode

Parameter examples:

  • START = get first element
  • CURRENT = returns current element
  • NEXT = returns next element
Returns
If found, the MemoryItem object, otherwise NULL_PTR.
Attention
The calling instance must not remove the supplied object from memory.

◆ insertSpyObject()

void MemSpy::LeakMap::insertSpyObject ( void * pNewObject,
char const * chFileName,
int iLine )

Inserts a MemoryItem object into the list of MemoryItem objects.

Parameters
[in]pNewObjectThe pointer to the new object to be inserted.
[in]chFileNameThe name of the file in which the new object was created.
[in]iLineThe line number in which the new object was created.

◆ leakMapUseAllowed()

bool MemSpy::LeakMap::leakMapUseAllowed ( ) const

Called in operator new( size_t, const char *, int ) or operator delete( void * ) to determine if LeakMap should be used.

Returns
If true, the newly instantiated or removed from memory object is processed by LeakMap.
See also
ms_bUseLeakMap

◆ numberOfProcessedObjects()

unsigned long long MemSpy::LeakMap::numberOfProcessedObjects ( ) const

Returns the number of objects processed so far.

Returns
See above.

◆ operator=()

LeakMap & MemSpy::LeakMap::operator= ( const LeakMap & )
private

The assignment operator is not available.

◆ removeSpyObject()

void MemSpy::LeakMap::removeSpyObject ( void * pObjectToDelete)

Removes a MemoryItem object from the list of MemoryItem objects.

Parameters
[in]pObjectToDeleteReference to the object to delete.

◆ setLeakMapUseAllowed()

void MemSpy::LeakMap::setLeakMapUseAllowed ( bool bAllowed = true)

Called in MemSpy::finishMemoryMonitoring (...) to disable memory monitoring.

Parameters
[in]bAllowedIf true, memory monitoring is enabled, otherwise disabled.
See also
ms_bUseLeakMap

◆ sizeOfObjectList()

std::size_t MemSpy::LeakMap::sizeOfObjectList ( ) const

Retrurns the number of entries in m_ObjectList.

Returns
See above.

Member Data Documentation

◆ m_LocalCriticalSection

CRITICAL_SECTION MemSpy::LeakMap::m_LocalCriticalSection
private

To have thread-safe access to the list of objects, access is synchronized via this local mutex.

◆ m_ObjectList

std::unordered_map< INT_PTR, MemoryItem * > MemSpy::LeakMap::m_ObjectList
private

The list of MemoryItem objects. The index is the address, cast to INT_PTR, of the individual objects.

◆ m_ObjectListIterator

std::unordered_map<INT_PTR,MemoryItem*>::iterator MemSpy::LeakMap::m_ObjectListIterator
private

An internal iterator to the MemoryItem object list.

◆ m_ullInsertCounter

unsigned long long MemSpy::LeakMap::m_ullInsertCounter
private

A counter that is incremented for each inserted object.

◆ ms_bUseLeakMap

bool MemSpy::LeakMap::ms_bUseLeakMap
staticprivate

Only if true, LeakMap is used in new (...) and delete (...). Is necessary to not recursively process own calls of new and delete here.

◆ ms_iReferenceCounter

int MemSpy::LeakMap::ms_iReferenceCounter
staticprivate

The reference counter to the only instance of this class.


The documentation for this class was generated from the following file: