Page 1 of 1

Access violation when deleting bodies

Posted: Fri Jul 17, 2015 9:13 pm
by rishflab
I am trying to delete a body using the same code as the collision example but I am getting an access violation error on the last line when calling cpBodyFree(). Anyone know whats up?

cpSpaceRemoveShape(space, shape);
cpSpaceRemoveBody(space, cpShapeGetBody(shape));


cpShapeFree(shape);
cpBodyFree(cpShapeGetBody(shape));

Re: Access violation when deleting bodies

Posted: Sat Sep 26, 2015 3:31 pm
by Amral
Hi Rishflab,

You're freeing the shape, and then accessing it to get the body, which is wrong. You shouldn't access the shape once you've freed it.

Try the following instead :

Code: Select all

cpBody *body = cpShapeGetBody(shape);

cpSpaceRemoveShape(space, shape);
cpSpaceRemoveBody(space, body);

cpShapeFree(shape);
cpBodyFree(body);