Adding a shape to a space twice shouldn't crash (?)

Discuss any Chipmunk bugs here.
Post Reply
tolmasky
Posts: 10
Joined: Wed Nov 16, 2011 4:52 am
Contact:

Adding a shape to a space twice shouldn't crash (?)

Post by tolmasky »

I find it weird that these two seemingly similar lines produce one working code sample, and one crashing code sample:

AOK:
body = cpBodyNew(mass, moment);
cpSpaceAddBody(space, body);

shape = cpBoxShapeNew(body, button.width, button.height);
cpSpaceAddShape(space, shape);

Crash:
body = cpBodyNew(mass, moment);
cpSpaceAddBody(space, body);

// assume shape got created somewhere else, thus we are using init...
cpBoxShapeInit(shape, body, width, height);
cpSpaceAddShape(space, shape);

The crash happens due to a failing assert that shape is not already added to the space. Init seems to check if the body has a space, and add the shape into that space if it does (from what I can tell). That's fine I guess, but is it really necessary to bail if you then try to add the shape yourself? I an understand if the shape belongs to a *different* space, but there seems to be no harm in adding a shape twice (knowingly or not, as is this case) to a space, you can just early return instead of asserting.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Adding a shape to a space twice shouldn't crash (?)

Post by slembcke »

In Chipmunk 6 at least that shouldn't ever be an error. Initializing the shape set's the private space field to NULL. On the other hand it would be very bad if you were accidentally re-initializing a shape that is already in use.

You can't add/remove objects more than once because it sets up and destroys relationships between objects when you do and can also trigger events. It would allow all sorts of fun and confusing problems if it was allowed. If you want you can always turn the assertions into early exits instead. Attempting to add or remove something more than once is an error in my code I want to know about.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tolmasky
Posts: 10
Joined: Wed Nov 16, 2011 4:52 am
Contact:

Re: Adding a shape to a space twice shouldn't crash (?)

Post by tolmasky »

I am using Chipmunk 6.0.2. I suppose I should rephrase the question to:

1. Do these code snippets behave the same for you in 6.0.2 -- in other words, does one work correctly and the other assert for you, and
2. If they do, do you think it is indeed correct behavior that the second code snippet should assert? (for clarification, it is not that in the second code snippet the shape has already been inited so I'm doing a double init or anything, it is just that here I am choosing to do cpPolyShapeAlloc and cpShapeInit instead of cpShapeNew). Perhaps it would have been clearer had I shown this:

body = cpBodyNew(mass, moment);
cpSpaceAddBody(space, body);

// assume shape got created somewhere else, thus we are using init...
shape = cpPolyShapeAlloc();
cpBoxShapeInit(shape, body, width, height);
cpSpaceAddShape(space, shape); // assert
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Adding a shape to a space twice shouldn't crash (?)

Post by slembcke »

A freshly initialized shape should not be triggering an assertion no. I'm actually not even sure how it's possible for that to happen.

Initializing a shape should set it's space pointer to NULL, and the assertion only triggers when space is not NULL.

Can you double check in the debugger that cpShape.space[_private] is actually NULL after initializing the shape and post some more info on which assertion is triggering? Like copy paste what it prints to the log.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests