Post Step Key Not working.

Official forum for the Chipmunk2D Physics Library.
Post Reply
Scarlet
Posts: 13
Joined: Tue Aug 16, 2011 2:55 pm
Contact:

Post Step Key Not working.

Post by Scarlet »

the space is adding 2 post steps for the same Key. This separate callback gets called twice, and registers the postStep Twice. Even though I only want one poststepCallBack. I also get this error message in the console

"Adding a post-step callback when the space is not locked is unnecessary. Post-step callbacks will not called until the end of the next call to cpSpaceStep() or the next query."

But inside the separate is the only place where I have added a post step CallBack? Is the Separate being called outside cpSpaceStep?

I fixed it by putting an if statement in PostStepCallBack, but why doesn't the key Work?

Code: Select all

void seperateFireBallandWall(cpArbiter *arb, cpSpace *space, void*data){
    Level1Layer* level = (Level1Layer*)space->data;
    CP_ARBITER_GET_BODIES(arb, fireBody, WallBody);
    CP_ARBITER_GET_SHAPES(arb, fireShape, WallShape);
    NSLog(@"fireBallSeperated");
    cpSpaceAddPostStepCallback(space, postStepRemoveFireBall, fireBody, fireShape);
}


void postStepRemoveFireBall(cpSpace *space, void *obj, void *data){
    cpBody *body = (cpBody*)obj;

    if (!cpSpaceContainsBody(space, body)) {
        return;
    }
    Level1Layer* level = (Level1Layer*)space->data;
    cpShape *shape = (cpShape*)data;
    CCSprite *fireBall = (CCSprite*)body->data;
    [level.dollBatchNode removeChild:fireBall cleanup:YES];
    level.spaceShipOnScreen = NO;
    cpSpaceRemoveBody(space, body);
    cpSpaceRemoveShape(space, shape);
    cpBodyFree(body);
    cpShapeFree(shape);

}
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Post Step Key Not working.

Post by slembcke »

The post step callbacks are actually stored in a hashset using the obj pointer you give it. I suspect that there is something else goofy going on here. Is it possible that the second separate callback is being called because you are removing a shape? That might be able to explain your bug... I'll have to make a test case to check if it's possible to occur or not.

What version of Chipmunk are you using? Can you:
1) Add a log statement to the post-step callback to double check that it's really the same object.
2) Put a breakpoint on the cpSpaceAddPostStepCallback() line and post the stack traces for both times it's called?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Scarlet
Posts: 13
Joined: Tue Aug 16, 2011 2:55 pm
Contact:

Re: Post Step Key Not working.

Post by Scarlet »

Here Are the Stack Traces for the first and second time. I set a breakPoint at cpSpaceAddPostStepCallBack not in it.

Please tell me if you need more info. Oh and the object key is the same both times, but the second call to separate comes inside the first poststepcallBack.
.
This is the first time:

#0 seperateFireBallandWall (arb=0x7343d20, space=0xfcf0f00, data=0x0) at Callbacks.m:324
#1 0x000e886a in cpArbiterCallSeparate (arb=0x7343d20, space=0xfcf0f00) at chipmunk_private.h:219
#2 0x000e8723 in cpSpaceArbiterSetFilter (arb=0x7343d20, space=0xfcf0f00) at cpSpaceStep.c:316
#3 0x000dc433 in cpHashSetFilter (set=0xfcf1210, func=0xe8680 <cpSpaceArbiterSetFilter>, data=0xfcf0f00) at cpHashSet.c:244
#4 0x000e8b1d in cpSpaceStep (space=0xfcf0f00, dt=0.0166666675) at cpSpaceStep.c:387
#5 0x000f19e0 in -[Level1Layer update:] (self=0x61780c0, _cmd=0x140270, dt=0.0166459996) at Level1Layer.m:536
#6 0x00064596 in -[CCScheduler tick:] (self=0xc266f00, _cmd=0x149632, dt=0.0166459996) at CCScheduler.m:583
#7 0x0008d6af in -[CCDirectorIOS drawScene] (self=0x6571990, _cmd=0x14397b) at CCDirectorIOS.m:152
#8 0x0008fc9a in -[CCDirectorDisplayLink mainLoop:] (self=0x6571990, _cmd=0x14f805, sender=0x6275360) at CCDirectorIOS.m:721
#9 0x00329a88 in CA::Display::DisplayLink::dispatch ()
#10 0x00329bcd in CA::Display::EmulatorDisplayLink::callback ()
#11 0x014da8c3 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
#12 0x014dbe74 in __CFRunLoopDoTimer ()
#13 0x014382c9 in __CFRunLoopRun ()
#14 0x01437840 in CFRunLoopRunSpecific ()
#15 0x01437761 in CFRunLoopRunInMode ()
#16 0x020131c4 in GSEventRunModal ()
#17 0x02013289 in GSEventRun ()
#18 0x00759c93 in UIApplicationMain ()
#19 0x000c04ff in main (argc=1, argv=0xbffff604) at main.m:14

This is the Second Time:
#0 seperateFireBallandWall (arb=0x7343b70, space=0xfcf0f00, data=0x0) at Callbacks.m:324
#1 0x000e261a in cpArbiterCallSeparate (arb=0x7343b70, space=0xfcf0f00) at chipmunk_private.h:219
#2 0x000e255f in cachedArbitersFilter (arb=0x7343b70, context=0xbfffe1a8) at cpSpace.c:340
#3 0x000dc433 in cpHashSetFilter (set=0xfcf1210, func=0xe24d0 <cachedArbitersFilter>, data=0xbfffe1a8) at cpHashSet.c:244
#4 0x000e26e0 in cpSpaceFilterArbiters (space=0xfcf0f00, body=0xfcc56a0, filter=0xfcfb000) at cpSpace.c:356
#5 0x000e27c1 in cpSpaceRemoveShape (space=0xfcf0f00, shape=0xfcfb000) at cpSpace.c:371
#6 0x0013b9bd in postStepRemoveFireBall (space=0xfcf0f00, obj=0xfcc56a0, data=0xfcfb000) at Callbacks.m:340
#7 0x000e7ca4 in cpSpacePostStepCallbackSetIter (callback=0x618ac60, space=0xfcf0f00) at cpSpaceStep.c:80
#8 0x000dc39f in cpHashSetEach (set=0x618a7a0, func=0xe7c70 <cpSpacePostStepCallbackSetIter>, data=0xfcf0f00) at cpHashSet.c:228
#9 0x000e7d10 in cpSpaceRunPostStepCallbacks (space=0xfcf0f00) at cpSpaceStep.c:92
#10 0x000e7e2d in cpSpaceUnlock (space=0xfcf0f00, runPostStep=1) at cpSpaceStep.c:119
#11 0x000e8f6f in cpSpaceStep (space=0xfcf0f00, dt=0.0166666675) at cpSpaceStep.c:452
#12 0x000f19e0 in -[Level1Layer update:] (self=0x61780c0, _cmd=0x140270, dt=0.0166459996) at Level1Layer.m:536
#13 0x00064596 in -[CCScheduler tick:] (self=0xc266f00, _cmd=0x149632, dt=0.0166459996) at CCScheduler.m:583
#14 0x0008d6af in -[CCDirectorIOS drawScene] (self=0x6571990, _cmd=0x14397b) at CCDirectorIOS.m:152
#15 0x0008fc9a in -[CCDirectorDisplayLink mainLoop:] (self=0x6571990, _cmd=0x14f805, sender=0x6275360) at CCDirectorIOS.m:721
#16 0x00329a88 in CA::Display::DisplayLink::dispatch ()
#17 0x00329bcd in CA::Display::EmulatorDisplayLink::callback ()
#18 0x014da8c3 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
#19 0x014dbe74 in __CFRunLoopDoTimer ()
#20 0x014382c9 in __CFRunLoopRun ()
#21 0x01437840 in CFRunLoopRunSpecific ()
#22 0x01437761 in CFRunLoopRunInMode ()
#23 0x020131c4 in GSEventRunModal ()
#24 0x02013289 in GSEventRun ()
#25 0x00759c93 in UIApplicationMain ()
#26 0x000c04ff in main (argc=1, argv=0xbffff604) at main.m:14
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Post Step Key Not working.

Post by slembcke »

Aha. I was afraid of that. In the second one, the separate callback is being called from cpSpaceRemoveShape(). It's a weird edge case that I needed to handle so that separate callbacks get called just before removing a shape from a space. If it waited until the next frame, you probably would have already freed the shape involved and would have been left with a dangling pointer.

Because post-step callbacks may trigger new separate callbacks, and those separate callbacks may add new post step callbacks, Chipmunk creates a fresh hashset before calling any of the post-step callbacks it already has. That way it's not mutating the hashset it's trying to iterate. The problem you have is that you can end up inserting a second callback on that same key. The warning is probably because I moved some of the lockingaround in cpSpaceStep() recently.

I'm going to have to think about how to fix that one. :-\
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Scarlet
Posts: 13
Joined: Tue Aug 16, 2011 2:55 pm
Contact:

Re: Post Step Key Not working.

Post by Scarlet »

OK thanks. By the way I think it should documented that cpSpaceRemoveShape() triggers separate callBacks.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Post Step Key Not working.

Post by slembcke »

I could swear that it was... but I can't find mention of it anywhere in the docs now. Hmm. Maybe it got removed when I revised things for Chipmunk 6.
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: Heise IT-Markt [Crawler] and 13 guests