SIGABRT crash on cpSpace:remove

Official forum for the Chipmunk2D Physics Library.
Post Reply
csoul
Posts: 56
Joined: Mon Mar 25, 2013 11:57 pm
Contact:

SIGABRT crash on cpSpace:remove

Post by csoul »

Once in a while my game will crash on the line indicated below:

Code: Select all

- (bool)beginProjectileEnemyCollision:(cpArbiter*)arbiter space:(ChipmunkSpace*)space
{
    CHIPMUNK_ARBITER_GET_SHAPES(arbiter, proj, en);
	
    Projectile *projectile = proj.data;
    Enemy *enemy = en.data;
    
    __unsafe_unretained typeof(*self) *weakSelf = self;
    
    [self.space addPostStepBlock:^{
        for(CCNode *node in projectile.sprites){
            [weakSelf removeChild:node cleanup:TRUE];
        }
        [weakSelf.space remove:projectile];
        [weakSelf.projectiles removeObject:projectile];
        
        ...
         for(CCNode *node in enemy.sprites){
                [weakSelf removeChild:node cleanup:TRUE];
            }
            
            [weakSelf.space remove:enemy]; //THIS LINE CAUSES THE CRASH
            [weakSelf.enemies removeObject:enemy];
        
        
    } key:projectile];
    
    
	return YES;
}
It only happens once in a while and is not easily reproducible, but it happens often enough. What I'm wondering is not only why it might occur, but if there's some graceful way to catch this so that the app doesn't completely crash.


Here's the full crash dump:
0com.apple.main-thread Crashed

Code: Select all

0 ...	 libsystem_kernel.dylib	 __pthread_kill + 8
2	 libsystem_c.dylib	 abort + 94
3	 MyApp	 _mh_execute_header + 1195807
4	 MyApp 	
cpSpace.c line 398
cpSpaceRemoveBody
5	 MyApp	
ChipmunkSpace.m line 527
-[ChipmunkSpace removeBody:]
6	 MyApp	
ChipmunkBody.m line 149
-[ChipmunkBody removeFromSpace:]
7	 MyApp	
ChipmunkSpace.m line 271
-[ChipmunkSpace remove:]
8	 MyApp	
ChipmunkSpace.m line 273
-[ChipmunkSpace remove:]
9	 MyApp	
AST_GameLayer.m line 484
__53-[AST_GameLayer beginProjectileEnemyCollision:space:]_block_invoke
10	 MyApp	
ChipmunkSpace.m line 343
postStepPerformBlock
11	 MyApp	
cpSpaceStep.c line 95
cpSpaceUnlock
12	 MyApp	
cpHastySpace.c line 452
cpHastySpaceStep
13	 MyApp	
ChipmunkHastySpace.m line 26
-[ChipmunkHastySpace step:]
14	 MyApp	
AST_GameLayer.m line 532
-[AST_GameLayer tick:]
15	 MyApp	
AST_GameLayer.m line 509
-[AST_GameLayer update:]
16	 MyApp	 -[CCScheduler update:] + 1008783
17	 MyApp	 -[CCDirectorIOS drawScene] + 1090949
18	 QuartzCore	 CA::Display::DisplayLink::dispatch(unsigned long long, unsigned long long) + 166
19	 QuartzCore	 CA::Display::IOMFBDisplayLink::callback(__IOMobileFramebuffer*, unsigned long long, unsigned long long, unsigned long long, void*) + 60
20	 IOMobileFramebuffer	 IOMobileFramebufferVsyncNotifyFunc + 156
21	 IOKit	 IODispatchCalloutFromCFMessage + 188
22 ...	 CoreFoundation	 __CFMachPortPerform + 362
29	 UIKit	 UIApplicationMain + 1080
30	 MyApp	
main.m line 14
main
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: SIGABRT crash on cpSpace:remove

Post by slembcke »

SIGABRT means that the program is intentionally halting the program. In this case, you are hitting one of the two assertions in cpSpaceRemoveBody(). Check the log, there should be a nice descriptive assertion error about what you did. ;)

The two possibilities are that the space was still locked or that you are trying to remove a body from the space twice. Since you are inside a post-step block, it's going to be the latter unless something is terribly wrong. The "key" value you use with post-step callbacks is supposed to help with that, to make sure you can't add two blocks that operate on the same object. Now, since your post-step block is keyed on the projectile, I'd guess that your "enemy" is getting hit by multiple projectiles at the same time. My solution to similar problems is to create two post-step callbacks. One for the projectile and one for the enemy.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
csoul
Posts: 56
Joined: Mon Mar 25, 2013 11:57 pm
Contact:

Re: SIGABRT crash on cpSpace:remove

Post by csoul »

Thanks for the info. So are you saying I should do this?

Code: Select all

[self.space addPostStepBlock:^{
        for(CCNode *node in projectile.sprites){
            [weakSelf removeChild:node cleanup:TRUE];
        }
        [weakSelf.space remove:projectile];
        [weakSelf.projectiles removeObject:projectile];
        
    } key:projectile];

[self.space addPostStepBlock:^{
        
         for(CCNode *node in enemy.sprites){
                [weakSelf removeChild:node cleanup:TRUE];
            }
            
            [weakSelf.space remove:enemy];
            [weakSelf.enemies removeObject:enemy];
        
    } key:enemy];
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: SIGABRT crash on cpSpace:remove

Post by slembcke »

Yes. Although a lot of my conclusions were inferred. I might have been wrong about the cause.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tetraweb
Posts: 31
Joined: Wed Feb 13, 2013 8:46 pm
Contact:

Re: SIGABRT crash on cpSpace:remove

Post by tetraweb »

@csoul This sounds similar to what @slembcke helped me figure out here:
http://chipmunk-physics.net/forum/viewt ... f=1&t=2872

Greg
csoul
Posts: 56
Joined: Mon Mar 25, 2013 11:57 pm
Contact:

Re: SIGABRT crash on cpSpace:remove

Post by csoul »

Thanks. It's difficult to tell exactly if this fixed it because it happens unpredictably, but I can say that it hasn't happened since I added the 2nd postStepBlock.
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests