Product Home Class IndexDownloadLicense

Object Ownership

This page discusses the concept of object ownership, and describes how it is used in the Chilkat SDK. Ownership is not specifiic to the Chilkat SDK and is commonly used by C++ developers.

If an object A owns another object B, then A is responsible for deleting B when A is deleted. This means that once object B is owned, the application should never delete B. This will cause the application to crash in object A's destructor because A will be trying to delete an already deleted object. Typically, an application should not keep any references or pointers to an owned object.

Some classes will allow the application to obtain a pointer to an owned object. Wherever this occurs, the pointer returned will always be a const pointer. It is impossible for an application to delete an object using a const pointer, unless of course the application decides to re-cast to a non-const pointer, which it should never do.

Some classes will allow an owned object to be extracted, and no longer owned. When this is the case, the pointer returned is not a const pointer because the object is no longer owned. At that point it is up to the application to decide what to do with the object, and the application is responsible for deleting it.

Ownership as used in CK_AnimatedImage

As an example, the CK_AnimatedImage class owns each CK_ImageFrame that comprises the animation. A const pointer to any given frame can be obtained with getFrameAt(). A frame can be extracted from the animation using extractFrameAt(). A frame can be inserted using insertFrameAt() or appendFrame(). Once the frame is inserted, the CK_AnimatedImage has ownership of the frame.