Access violation when deleting bodies

Official forum for the Chipmunk2D Physics Library.
Post Reply
rishflab
Posts: 3
Joined: Sun Jul 12, 2015 8:12 pm
Contact:

Access violation when deleting bodies

Post 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));
Amral
Posts: 5
Joined: Fri Oct 24, 2014 11:11 am
Contact:

Re: Access violation when deleting bodies

Post 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);
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests