active2staticIter crashing application

Official forum for the Chipmunk2D Physics Library.
bdittmer
Posts: 6
Joined: Tue Sep 01, 2009 8:53 am
Contact:

active2staticIter crashing application

Post by bdittmer »

Hello, I was wondering if anyone is experiencing issues with chipmunk2d apps crashing in the active2staticIter function inside of cpSpace.c. It appears that a dangling pointer being passed into the function and causing the crash. Below is a stack trace of the relevant chipmunk functions:

0 0x0000caf4 active2staticIter (cpSpace.c:437)
1 0x0000d748 eachHelper (cpSpaceHash.c:324)
2 0x0000b3e6 cpHashSetEach (cpHashSet.c:192)
3 0x0000d786 cpSpaceHashEach (cpSpaceHash.c:334)
4 0x0000cc52 cpSpaceStep (cpSpace.c:479)

My application is actively removing shapes/bodies from the space from inside collisions (the shapes/bodies are being removed, not free'd - they are added back in later) . What I assume is happening is cpSpaceStep is being called, a collision happens and a shape is removed from the space, and so the pointer to that shape passed into active2staticIter is no longer valid. Does this sound about right? If so does anyone have any solutions or work arounds? I can post code if necessary. Thanks!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: active2staticIter crashing application

Post by slembcke »

What version? There were 5 or so dangling pointer bugs that would cause that to crash in 4.x if you were removing shapes inside a callback. Still not really sure if it was worth the effort either as doing so in a GCed language could still end up freeing the shape/body and cause a crash.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
bdittmer
Posts: 6
Joined: Tue Sep 01, 2009 8:53 am
Contact:

Re: active2staticIter crashing application

Post by bdittmer »

It looks like version 4 - it shipped with cocos2d .8.0. I am unable to find any version number in the chipmunk source or listed in the cocos documentation, but based on the ReadMe in the chipmunk directory, 4.0 is a good a guess. Should I try upgrading to the latest version? Since this is obviously an iPhone app a GC shouldn't be any trouble :)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: active2staticIter crashing application

Post by slembcke »

Well. That actually is the latest released version. I've made a ton of changes in trunk, but have been sitting on them for a long time as I haven't had the time to make a proper release for it.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
bdittmer
Posts: 6
Joined: Tue Sep 01, 2009 8:53 am
Contact:

Re: active2staticIter crashing application

Post by bdittmer »

Hrmm...well here is the relevant code, can you spot any glaring issues?

Code: Select all

- (void) step: (ccTime) delta {
	int steps = 2;
	CGFloat dt = delta/(CGFloat)steps;

	for(int i=0; i<steps; i++) {
		cpSpaceStep(space, dt);	
	}

	cpSpaceHashEach(space->activeShapes, &eachShape, nil);

	// The forces on the puck need reset every step, otherwise they will accumulate
	cpBodyResetForces(myBody);
}
This method is called by cocos2d every frame...could not using fixed timesteps be causing the issue?

Code: Select all

static int myShapeCollision(cpShape *myShape, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data) {
	GameLayer *ghl = (GameLayer*)data;

	cpSpaceRemoveBody(ghl.space, myShape->body);
	cpSpaceRemoveShape(ghl.space, myShape);
		
	return 1;
}
This is my collision-pair function which simply removes the shape from the space.

Any insight would be much appreciated!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: active2staticIter crashing application

Post by slembcke »

I think I was not very clear. 4.1.0 is the latest point release. I have a ton of changes in trunk in the SVN repository that have not had an official release. That is where I made the fixes for removing objects inside a callback.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
bdittmer
Posts: 6
Joined: Tue Sep 01, 2009 8:53 am
Contact:

Re: active2staticIter crashing application

Post by bdittmer »

Indeed I did misunderstand. :) Is trunk considered stable and ready for production use?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: active2staticIter crashing application

Post by slembcke »

bdittmer wrote:Is trunk considered stable and ready for production use?
Yeah, I rarely make changes that break anything. Mostly it's just that the documentation doesn't get the attention in needs.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
teedr
Posts: 4
Joined: Sun Nov 08, 2009 6:31 pm
Contact:

Re: active2staticIter crashing application

Post by teedr »

I am experiencing these same problems. Sorry, could you explain how I can work around it?

Thanks a lot.
zaerl
Posts: 40
Joined: Fri Aug 28, 2009 8:36 am
Contact:

Re: active2staticIter crashing application

Post by zaerl »

teedr wrote:I am experiencing these same problems. Sorry, could you explain how I can work around it?

Thanks a lot.
Don't remove shapes in collision callbacks.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests