Class Reference::HeapTracked

class HeapTracked

Determines if the instance was dynamically allocated on the heap.

The HeapTracked class overloads operator new in order to store the addresses of instances that have been dynamically allocated on the heap (see the HeapTracked class and the discussion in Item 27 of Scott Meyers, More Effective C++).

As the number of objects constructed at any one time will generally be much smaller than the number of objects in existence at the time of deletion, it is more efficient to search the heap addresses during construction and to set the __heap_state attribute accordingly. However, in order to handle the possibility of multiple inheritance, it is necessary for the __is_on_heap method to perform a dynamic cast of the “this” pointer before searching the heap addresses. As this dynamic cast does not work inside the constructor of a virtually inherited class, it is not possible to set the __heap_state attribute during construction.

Therefore, in order to improve the efficiency of the __is_on_heap search, it is advised to call the __is_on_heap method as soon as possible after construction of the object. This is done by the Reference::Able::__reference method, which is called by the Reference::To template class when its assigment operator is called.

Subclassed by Reference::Able

Public Functions

inline HeapTracked()

Default constructor.

inline HeapTracked(const HeapTracked&)

Copy constructor.

inline HeapTracked &operator=(const HeapTracked&)

Assignment operator.

virtual ~HeapTracked()

Destructor.

bool __is_on_heap() const

Return true if this instance is found in heap addresses.

Public Static Functions

static void *operator new(size_t size, void *ptr = 0)

Records the addresses of new instances allocated on the heap.

static void operator delete(void *location, void *ptr)

Placement delete overload to match the placement new overload.

static void operator delete(void *location)

Regular delete overload.

static size_t get_heap_queue_size()

Return the number of heap addresses awaiting processing.