monitorius Posted November 8, 2010 Posted November 8, 2010 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.
frustum Posted November 8, 2010 Posted November 8, 2010 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.
monitorius Posted November 9, 2010 Author Posted November 9, 2010 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++)?
Guest extaliones Posted November 12, 2010 Posted November 12, 2010 Unfortunately, virtual destructors in script are increase the time of the objects deleting and decrease performance, so we do not do it.
Recommended Posts