Page 1 of 1

cpShapeSetBody unsafe

Posted: Sun Apr 08, 2012 8:15 pm
by scribbleink
Hi Scott,

I'm using Chipmunk 6.0.1 in C and my use case is in this particular order (forgive my short-hand syntax):

Code: Select all

{
  shape[i] = cpPolyShapeNew(NULL, ...);     // create multiple shapes without a body
}
body = cpSpaceAddBody(...);                 // create a body later
{
  cpShapeSetBody(shape[i], body)           // set them to be part of the same body
  cpSpaceAddShape(shape[i])                // add them to the space
}
Now this should work since it follows the rules in the documentation in that I'm setting the Body of each Shape before adding it to the space.

However, I got a SIGSEGV and tracked it down to cpSpaceAddBody() calling an assertion cpShapeActive() which in the private API reads like the following around line 105 (chipmunk_private.h, 6.0.3):

Code: Select all

static inline cpBool
cpShapeActive(cpShape *shape)
{
	return shape->prev || shape->body->shapeList == shape;
}
When shape->body == NULL, this code terminates although the shape isn't active because it isn't attached to a body. Could you please change this to, perhaps the following?

Code: Select all

static inline cpBool
cpShapeActive(cpShape *shape)
{
	return shape->prev || (shape->body && shape->body->shapeList == shape);
}
There might be other ways to do this, and I know this isn't a typical use-case scenario, but I wanted to bring this to your notice. Keep up the good work!

Best,
Nikhil

Re: cpShapeSetBody unsafe

Posted: Mon Apr 09, 2012 8:38 am
by slembcke
Hmm. It looks like I might have fixed that in the current Git code. What version are you running. I remember something about that coming up a few months ago, but not what exactly. Somebody was doing something with serialization stuff maybe? Anyway, cpSpaceAddBody() looks like it should be safe anyway.

Re: cpShapeSetBody unsafe

Posted: Tue Apr 10, 2012 4:57 am
by scribbleink
slembcke wrote: It looks like I might have fixed that in the current Git code.
Yeah, it's fixed according to this. Sorry I didn't test the latest one like I should have (since we froze a previous version for our development purposes).
slembcke wrote:What version are you running.
It was 6.0.2.
slembcke wrote:I remember something about that coming up a few months ago, but not what exactly. Somebody was doing something with serialization stuff maybe?
I couldn't find anything in the forums at the time I posted this. After looking on the Git repo, this would be marked a duplicate of #2.

Thanks for your help.