THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 01/11/2008 at 17:13, xxxxxxxx wrote:
Undo stack or not, the only consistent way to retain a 'pointer' to an object in a document over any duration beyond a short functional scope is by using a BaseLink (and, yes, if the object is deleted from the document, the BaseLink will return NULL).
The only time that I use BaseObject* (for instance) is during a scope when working on the object in some functions (when there is no way for the user to delete it in the meantime - this may not always be the case if you are using threads!).
Two problems can arise trying to store a pointer to an object longterm:
1. It is deleted by the user and your pointer now points to invalid memory. C++ pointers require that you keep tabs on validity unless you are using something like a smart pointer system. Since you are not in control of the user activities, this is very difficult (a SceneHook may work but not recommended). I don't know about Undo behavior, but I doubt that doing an Undo would 'restore' the object to the exact same memory address. And, of course, C++ pointers have no relevance in file saves/loads. But BaseLinks can maintain reference even during saving/loading of a document.
2. In some places in the SDK, you receive a *copy* of the object! Or, worse, an object cache!! Never assume that your pointer is pointing to the actual object without being absolutely sure what you are getting (e.g.: reading the Docs when necessary to see what is returned by a call).
Basically, feel free to use a BaseObject* (or similar) when you are doing something involving it or a pointer is required for doing something else. But when you want to 'store' a reference to an object in a class for any longetivity, use a BaseLink of some sort (LINK description, GeData-BaseContainer BaseLink).