Problem removing object in Object chipmunk 5.3.3

Discuss any Chipmunk bugs here.
Post Reply
ganesha
Posts: 71
Joined: Mon Sep 06, 2010 1:29 pm
Contact:

Problem removing object in Object chipmunk 5.3.3

Post by ganesha »

Hi

I upgraded to Object chipmunk 5.3.3 and now I get this error (that never showed up before) when removing an object from the space.
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
Source:/Users/slembcke/Development/Objective-Chipmunk/../Chipmunk/src/cpSpace.c:305
ganesha
Posts: 71
Joined: Mon Sep 06, 2010 1:29 pm
Contact:

Re: Problem removing object in Object chipmunk 5.3.3

Post by ganesha »

This is the code. I call the addPostStepCallback:self method in the update method but can't see how that would affect the problem.

Code: Select all

-(void) removeObject: (id) object{
	[self.space remove:object];
}

Code: Select all

[self.space addPostStepCallback:self selector:@selector(removeObject:) context:imageObject];
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problem removing object in Object chipmunk 5.3.3

Post by slembcke »

Can you double check that? cpSpace.c line 305 is the space lock assertion from cpSpaceAddShape(). If you run it from the debugger it should show you a stack trace of where the assertion was triggered.

I made some of the assertions more strict after finding a couple of new ways to cause crashes from queries and cpBodyActivate().
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ganesha
Posts: 71
Joined: Mon Sep 06, 2010 1:29 pm
Contact:

Re: Problem removing object in Object chipmunk 5.3.3

Post by ganesha »

I have checked the code. The problem occurs when hitting the [space step] function the first time after calling addPostStepCallback. The app crashes without any stack trace.

The earlier log was from my attempt to skip the addPostStepCallback function (due to this error) and thus triggering the assert error. My post was a little bit unclear about this.

Order of execution:

1.[space step...] Executing fine in run loop
2.[self.space addPostStepCallback:self selector:@selector(addObjectToSpace:) context:object];
3.[space step...] crash

Method addObjectToSpace is never called.

Note: if the objects are added before the loop starts the [space add...] method works fine.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problem removing object in Object chipmunk 5.3.3

Post by slembcke »

Are you calling addPostStepCallback: outside of step:?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ganesha
Posts: 71
Joined: Mon Sep 06, 2010 1:29 pm
Contact:

Re: Problem removing object in Object chipmunk 5.3.3

Post by ganesha »

Both during step and outside. Both fail. Is this approach wrong?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problem removing object in Object chipmunk 5.3.3

Post by slembcke »

I ask because just a couple days ago I noticed 2 minor bugs in addPostStepCallback. Then yesterday I got the first two bug reports for it. /me facepalm Talk about unfortunate timing.

The first bug was that it was using the target instead of the context as the key value for the underlying call to cpPostStepCallback() which meant that you could only register once callback per target. The documentation also needed to be updated to make it more clear that this was happening too. The other bug is the one that you've hit. While you can call addPostStepCallback: from outside of step:, it wasn't really intended to done. If you aren't in a collision callback, there really is no need to use a post-step callback as their purpose is to allow you to defer removals from a collision callback until it's safe to do so. addPostStepCallback: didn't actually retain it's arguments, and nobody noticed until now. I only stumbled upon the issue by chance.

It's not really possible to fix the old method without breaking existing working code, so I've marked it as deprecated and written a new method:
- (void)addPostStepCallback:(id)target selector:(SEL)selector key:(id)key;

The new name reflects that the context value is actually a key, and I made sure the documentation is crystal clear this time about how it works. I also created some extra C APIs in Chipmunk to support managing the post-step callback memory, so now it doesn't make assumptions about the memory management. Lastly, I've written unit tests to cover it. Bugs begone!

I'm pushing to make sure I have all the new tests written by tomorrow or Monday so that I can send out the 5.3.4 update, but I can send you a dev build now if you want. What email did you use to purchase Objective-Chipmunk?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ganesha
Posts: 71
Joined: Mon Sep 06, 2010 1:29 pm
Contact:

Re: Problem removing object in Object chipmunk 5.3.3

Post by ganesha »

See PM.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests