Jump to content

Virtual destructors


photo

Recommended Posts

Posted

I've got an error "ExternClass::destructor(): object is not constructed" in that code:

 

int init() {	
ObjectMeshDynamic obj = new ObjectMeshDynamic();
obj.setBody(new BodyRigid());
Body body = obj.getBody();
delete body;
return 1;
}

 

But it's all right when class_cast is used:

int init() {	
ObjectMeshDynamic obj = new ObjectMeshDynamic();
obj.setBody(new BodyRigid());
BodyRigid body = class_cast("BodyRigid",obj.getBody());
delete body;
return 1;
}

 

It looks like a bug when objects can not be deleted through a pointer to a base class.

post-83-073862000 1289223829_thumb.jpg

Posted

This code is incorrect. You will obtain segmentation fault on ObjectMeshDynamic destruction:

 

int init() {	
ObjectMeshDynamic obj = new ObjectMeshDynamic();
obj.setBody(new BodyRigid());
Body body = obj.getBody();
delete body;
return 1;
}

 

Correct code is:

 

obj.setBody(class_remove(new BodyRigid()));

 

Object::setBody() function passes ownership of Body to the Object.

 

PS: body_cast() is a safe version of class_cast() function for bodies.

Posted
Object::setBody() function passes ownership of Body to the Object.

I didn't know that, thanks. Does that fact exist somewhere in manual?

 

 

Yeah, my example was bad. Now, the next one:

int init() {    
BodyRigid bodyR = new BodyRigid();	// get a pointer to BodyRigid-object
Body body = class_cast("Body",bodyR);	// get pointer to BodyRigid-object as a pointer to base class
delete body;				// try to delete an object through pointer to a base class 
					//--> error "ExternClass::destructor(): object is not constructed"
return 1;
}

 

So, my question was about destruction - can one delete object using pointer to a base class (like in C++)?

Posted

Unfortunately, virtual destructors in script are increase the time of the objects deleting and decrease performance, so we do not do it.

×
×
  • Create New...