Deleting/Removing PegThings

Removing a PegThing (i.e. a window, button, dialog, or other object derived from PegThing) from its parent is not the same as deleting it. Removing an object takes it out of the active display tree. After being removed, the object no longer has a parent, and it will not be visible. It is possible, even common, to later re-add the object to a visible PegThing and use it over again.

A PegThing is removed by calling the PegThing member function Remove(PegThing * What). It doesn't matter if the parent object removes a child, or if a child removes itself, because the Remove() function properly handles either case. That is, it is perfectly acceptable to use the following statement:

Remove(this);

when an object decides based on some message input that it is time to go away.

While Remove() can be useful, it is more common to want to both remove the object from its parent, as well as delete the object from memory. There are three acceptable ways to remove and delete an object:

Sending a PM_DESTROY message to PegPresentationManager. The pSource member of the PM_DESTROY message should point to the object which is to be destroyed. This method is most often used when deleting objects from tasks outside of RTPEG-32.

Calling the PegThing member function Destroy(PegThing * Who). Any PegThing can destroy any other PegThing, including itself. This does not mean that the Destroy() function will end up executing a delete(this) statement. The Destroy function checks to see if Who == this, and in this case automatically sends a PM_DESTROY message to PegPresentationManager to finish the job.

If a PegThing is already removed from its parent through use of the Remove() function, and the object to be deleted is not this, nor is this a child of the object being deleted, it is fine to simply delete the object.

One should never execute delete(this). When in doubt, it is always safe to call Destroy(). It is not necessary to manually delete the individual children of a PegThing, in fact it will cause errors if this is attempted.