#include <OTC/memory/mcreaper.hh> class OTC_MallocReaper {
public:
inline OTC_MallocReaper();
inline ~OTC_MallocReaper();
inline void grab(void* theData);
inline void release();
};
OTC_MallocReaper
assists in ensuring that data
allocated from the free store using malloc()
is deleted when the
stack is unwound as a result of an exception. This is achieved by
using an instance of this class as a handle to the data allocated
using malloc()
. Once the data is grabbed using an instance of
this class, the section of code in which an exception could be
raised is executed. If an exception does occur then the instance
of this class will be destroyed and the data freed. If no
exception occurs a call can be made to release the data, when the
destructor is finally called the data would not be freed.
void function()
{
OTC_MallocReaper xxxData;
char* theData = malloc(2);
OTCLIB_ASSERT(theData != 0);
xxxData.grab(theData);
... code which could throw an exception
xxxData.release();
}
inline OTC_MallocReaper();
0
.
inline ~OTC_MallocReaper();
0
, the data will be freed.
inline void grab(void* theData);
theData
. If the
handle was set to point at another block
of data, it will now point to the new data
and the first block of data will not be
changed.
inline void release();
0
. This is equivalent
to calling grab(0)
.
OTC_Reaper
, OTC_VecReaper