//--- example
extern bool make_error;
extern int error;
extern CArrayObj *src;
//--- create a new instance CArrayObj
//--- default memory management is turned on
CArrayObj *array=new CArrayObj;
//--- add (copy) the elements from the source array
if(array!=NULL)
bool result=array.AddArray(src);
if(make_error)
{
//--- perform erroneous actions
switch(error)
{
case 0:
//--- remove the source array without checking its memory management flag
delete src;
//--- result:
//--- it is possible to address an element by invalid pointer in the receiver array
break;
case 1:
//--- disable the mechanism of memory management in the source array
if(src.FreeMode()) src.FreeMode(false);
//--- but do not remove the source array
//--- result:
//--- after removing the receiver array, it is possible to address an element by invalid pointer in the source array
break;
case 2:
//--- disable the mechanism of memory management in the source array
src.FreeMode(false);
//--- disable the mechanism of memory management in the receiver array
array.FreeMode(false);
//--- result:
//--- after the program termination, get a "memory leak"
break;
}
}
else
{
//--- disable the mechanism of memory management in the source array
if(src.FreeMode()) src.FreeMode(false);
//--- delete the source array
delete src;
//--- result:
//--- addressing the receiver array element will be correct
//--- deleting the receiver array will lead to deleting its elements
}
|