A heap can be checked to make sure that its allocated and free cells are in a consistent state and that the heap is not corrupt.
The THeapWalk
class provides the behaviour for doing
this. This class is pure virtual and developers must provide a derived class in
order to use it.
Simply construct a THeapWalk
derived object passing a
reference to the heap which is to be checked in its constructor. Walking the
heap is initiated by calling Walk()
. This function follows the
list of allocated and free cells in the heap. The function calls the virtual
function Info()
for every good cell it finds, passing the cell's
address and length.
Info()
is a pure virtual function and a derived class
must provide an implementation for it.
Walk()
terminates when it has successfully followed the
entire list of free and allocated cells or until it finds the first bad
cell.
As a minimum, a class derived from THeapWalk
might
be:
class TMyClass : public THeapWalk
{
public :
TMyClass(RHeap& aHeap);
void Info(TCellType aType,TAny *aBase,TInt aLength);
}
where the constructor would be implemented as:
TMyClass::TMyClass(RHeap& aHeap)
: THeapWalk(aHeap)
{}
Typically, the Info()
function might consist of a switch
statement based on the value of aType
:
TMyClass::Info(TCellType aType,TAny *aBase,TInt aLength)
{
switch(aType)
{
case EGoodAllocatedCell:
...
break;
case EGoodFreeCell:
...
break;
case EBadFreeCellAddress:
...
break;
default:
...
}
}