I can't remove constraint in chipmunk 6

Official forum for the Chipmunk2D Physics Library.
Post Reply
Panos
Posts: 5
Joined: Thu Mar 10, 2011 1:51 pm
Contact:

I can't remove constraint in chipmunk 6

Post by Panos »

Hello, I am having a problem in chipmunk 6 when I try to remove constraints. In 5 I was using this to remove my constraints and it worked.

Code: Select all

cpSpaceRemoveConstraint(space, monster.pivot);
cpSpaceRemoveConstraint(space, monster.gear);
But in 6 I get
Aborting due to Chipmunk error: This addition/removal cannot be done safely during a call to cpSpaceStep() or during a query. Put these calls into a post-step callback.
Failed condition: !space->locked
I used a post-step callback and I get
Program received signal: “EXC_BAD_ACCESS”.
it seems to stop in this function

http://tinypic.com/view.php?pic=23syaee&s=7

How can I remove the constraints?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: I can't remove constraint in chipmunk 6

Post by slembcke »

post-solve or post-step? Can you post more of the code or a stack trace? The more information you can share the better.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: I can't remove constraint in chipmunk 6

Post by slembcke »

Oh. I think I understand better now. You always had to use a post-step callback to remove things from a collision callback (even in 5.x). Doing otherwise is guaranteed to cause rare, random, and hard to track down crashes. The assertion existed in 5.x too, but only when compiled in debug mode. In Chipmunk 6.x I changed a bunch of the really important assertions that didn't happen enough to affect performance to always be run. That way people couldn't ignore or miss them. ;)

Now, for the crash. The crash is almost certainly because you already freed the body the constraint was attached to before removing the constraint. Are you doing something like this:

Code: Select all

cpSpaceRemoveBody(space, body);
cpBodyFree(body);

cpSpaceConstraintRemove(space, constraintAttachedToTheSameBody); // CRASH!
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Panos
Posts: 5
Joined: Thu Mar 10, 2011 1:51 pm
Contact:

Re: I can't remove constraint in chipmunk 6

Post by Panos »

Code: Select all

for (i = 0; i<[gameController.game.currentLevel.monsterArray count]; i++) {
		Monster * monster =	[gameController.game.currentLevel.monsterArray objectAtIndex:i];
		//cpSpaceRemoveConstraint(space, monster.pivot);
		//cpSpaceRemoveConstraint(space, monster.gear);
		cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepConstraintRemove, monster.pivot, NULL);
		cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepConstraintRemove, monster.gear, NULL);
		cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepActiveShapeRemove, monster.view, NULL);
		cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepActiveRemove, monster.shape, NULL);
		cpBodyFree(monster.controlBody);
		[monster.sprite removeFromParentAndCleanup:YES];
		[monster.shadow removeFromParentAndCleanup:YES];
		
	}
the commented part is what I was using in chipmunk 5. My objects are similar to the objects in tank demo. I just added a sensor shape to act as the monsters view.

and these are the post-step functions

Code: Select all

static void postStepActiveRemove(cpSpace *space, cpShape *shape, void *unused)//removes body and shape
{	
	cpSpaceRemoveBody(space, shape->body);
	cpBodyFree(shape->body);

	cpSpaceRemoveShape(space, shape);
	cpShapeFree(shape);
}

static void postStepConstraintRemove(cpSpace *space, cpConstraint *constraint, void *unused)
{	
	cpSpaceRemoveConstraint(space, constraint);
}

static void postStepActiveShapeRemove(cpSpace *space, cpShape *shape, void *unused)//removes shape only
{	
	cpSpaceRemoveShape(space, shape);
	cpShapeFree(shape);
}
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: I can't remove constraint in chipmunk 6

Post by slembcke »

I see the problem. The post-step functions are stored in a hash set and not called in any particular order. Your body is potentially being freed before the constraints.

You should make a single post-step callback instead of 4. I'm assuming you did that to make the code more reusable. A better solution would be to make a post-step callback that calls a block.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Panos
Posts: 5
Joined: Thu Mar 10, 2011 1:51 pm
Contact:

Re: I can't remove constraint in chipmunk 6

Post by Panos »

Yes I use these functions for other shapes too. I could change it and have one for each type of object. I imagined it was freeing the body first and then the constraint...

Should I use cpBodyEachConstraint() and cpBodyEachShape() inside my post-step callback? Would this work?
Panos
Posts: 5
Joined: Thu Mar 10, 2011 1:51 pm
Contact:

Re: I can't remove constraint in chipmunk 6

Post by Panos »

I made it... Why does my sensor disappear too? I didn't add

cpSpaceRemoveShape(space, monster.view);
cpShapeFree(monster.view);

but it is removed too...

Code: Select all

cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepMonsterRemove,monster.shape, monster);

Code: Select all

static void postStepMonsterRemove(cpSpace *space, cpShape *shape, Monster *monster)
{
	
	cpSpaceRemoveConstraint(space, monster.pivot);
	cpSpaceRemoveConstraint(space, monster.gear);
	cpSpaceRemoveBody(space, monster.body);
	cpBodyFree(monster.body);
	cpBodyFree(monster.controlBody);
	
	cpSpaceRemoveShape(space, shape);
	cpShapeFree(shape);
}
}
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: I can't remove constraint in chipmunk 6

Post by slembcke »

You are freeing the body it's attached to. The next frame when it updates the shape it's probably getting garbage for the position/rotation and putting it off in the middle of nowhere. If you ran it with guard malloc turned on you should get a hard crash instead.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Panos
Posts: 5
Joined: Thu Mar 10, 2011 1:51 pm
Contact:

Re: I can't remove constraint in chipmunk 6

Post by Panos »

I see. Thanks for the help. Chipmunk is great.
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests