Zen API
|
There are two ways to destroy an object derived from the kObject class: kObject_Destroy and kObject_Dispose. Both of these methods will destroy the object itself, but kObject_Dispose will also destroy any aggregated child objects.
The purpose of kObject_Dispose is to allow arbitrary hierarchies of connected objects to be deallocated in a single call, rather than having to traverse the hierarchy, destroying each object in turn. For example, consider a list object that contains images (kArrayList<kImage>). The list class represents a data collection; it contains references to images that have been added to the collection, but it does not own the image objects. kObject_Destroy will destroy the list but will not automatically destroy the list elements (in this case, images). kObject_Dispose will destroy both the list object and the images contained in the list:
The difference between kObject_Dispose and kObject_Destroy is best understood in terms of the relationship between the parent object and its children. If the parent object strictly owns its children (composition), then the children will always be destroyed when the parent is destroyed. But if the parent merely contains its children (aggregation), then the children will only be destroyed by using kObject_Dispose.
Most classes, aside from those designed to serve as data collections (e.g. kArrayList, kQueue), respond identically to kObject_Destroy or kObject_Dispose, because child objects are typically owned by the parent object. Refer to the documentation for each class to determine if it has aggregated child elements that can be destroyed using kObject_Dispose.
Note to implementers: If you are implementing a data collection that will support kObject_Dispose, remember to override the kObject_VDisposeItems method. The DisposeItems method is called by kObject_Dispose to handle child elements.