GetLink / Crash when deleting object

On 12/05/2013 at 12:32, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   r14 
Language(s) :     C++  ;

Hi, I am facing a problem which I dont understand..
this is my code to get a linked object:

ipCustomObject = bc->GetLink(IP_CUSTOM_OBJECT,doc);
if (!ipCustomObject) return FALSE;

I was thinking that this prevents further problems when the linked object is removed by the user, but it doesnt. Can anyone point out whats wrong?

later on in the code i use the linked object to create an instance. everything works well until the user deletes the object.

innerPillar = BaseObject::Alloc(Oinstance);


On 12/05/2013 at 16:55, xxxxxxxx wrote:

I would simply get the linked object at those points where you need to use it instead of storing it as a class member and hoping for the best.  If the user deletes the object or clears the link, you will get a NULL for GetLink() when you do need to use it and thereby avoid the crashes by using the condition at that point.  Also, you can use GetObjectLink() to avoid casting.

On 13/05/2013 at 00:12, xxxxxxxx wrote:

thank you, Robert. now it works.. but what about performance as it looks a bit strange like this now:

if (bc->GetObjectLink(IP_CUSTOM_OBJECT,doc)) {
innerPillar = BaseObject::Alloc(Oinstance);
innerPillar->SetParameter(DescID(INSTANCEOBJECT_LINK),GeData(ToPoly((PolygonObject* )(bc->GetObjectLink(IP_CUSTOM_OBJECT,doc)))),DESCFLAGS_SET_0);
innerPillar = ((BaseObject* )ToPoly((PolygonObject* )(bc->GetObjectLink(IP_CUSTOM_OBJECT,doc))))->GetRad();

maybe i need to get the parameters at another stage? now i am doing it like this:

Bool myPlugin::Message(GeListNode *node, LONG type, void *t_data)
		BaseObject *op = (BaseObject* )node;
	return TRUE;

On 13/05/2013 at 05:29, xxxxxxxx wrote:

Do it like this:

PolygonObject* ipCustomObject =    ToPoly(bc->GetObjectLink(IP_CUSTOM_OBJECT,doc));  
if (!(ipCustomObject && ipCustomObject->IsInstanceOf(Opolygon)))    return FALSE;  
innerPillar = BaseObject::Alloc(Oinstance);  
if (!innerPillar) return FALSE;  
innerPillar = ipCustomObject->GetRad(); // ????

Not sure what you are doing in the last line.

On 13/05/2013 at 09:20, xxxxxxxx wrote:

thank you for your help 🙂