THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 19/10/2008 at 10:09, xxxxxxxx wrote:
Cinema 4D Version: R11
Language(s) : C++ ;
in my code i have a pointer to a baseobject, thats inserted into the document.
Then i only do stuff with the object when that pointer is not NULL.
When I delete the object in the document, it passes the if(object!=NULL).. test and thus my code crashes...
This happens for me with R11, on R9 i have no crash with this issue..
Is there another way do detect if an object that i have a pointer to does stil exist ? or am i doing sth wrong?
I think i read somewhere its not so good to use global pointers anyway, so if i stored that as a link in a basecontainer, would that link become NULL, if the object was deleted from the doc ?
i think im gonna try that out now ..
On 19/10/2008 at 10:27, xxxxxxxx wrote:
ok i think that did it..
On 01/11/2008 at 15:09, xxxxxxxx wrote:
Well, looks like you've already found a solution, but I thought I'd mention that if you delete an object you're actually moving it onto the undo stack so the pointer to the object may still be valid.
An easier way to check if an object has been deleted is this:
> \> if(op->GetDocument() == doc) \>
\> if(op->GetDocument() == doc) \>
If the above statement returns TRUE, then the object is still in the document, and if it returns FALSE then the object is not in the document anymore and it may be on the undo stack. ;o)
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).
On 02/11/2008 at 05:26, xxxxxxxx wrote:
thanks, some interesting info there.
Im storing my object references as Links in BaseContainers now, so hopefully there shouldnt be anymore problems with invalid pointers after deleting, un-doing and so on.
On 02/11/2008 at 08:40, xxxxxxxx wrote:
Yes, as Robert mentioned, storing an object in a BaseContainer link is a consistent way to keep track of object pointers.
Just wanted to mention that if you have a valid pointer to an object, it doesn't always mean the object is in the document. ;o)