Cocos2d, iOS and XCode: how can I debug/understand

2019-09-19 18:14发布

问题:

I am developing a Cocos2d 2.0 game and can't quiet get around on why my App is crashing.

The console output is as following:

2013-02-08 10:52:08.298 AppName[994:15203] cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [29] to [40].
2013-02-08 10:52:08.299 AppName[994:15203] cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [40] to [54].
2013-02-08 10:52:08.300 AppName[994:15203] cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [54] to [73].
(lldb) 

The "game scene" loads and crashes immediately. I can see that has to do with CCSpriteBatchNode but I have absolutely no idea on where exactly the code fails. If I add a breakpoint it is not of much use as it will trigger way to many times before reaching the exact moment when it fails.

What does (lldb) mean? Is there a way to have a more explicit stacktrace? I used to work in Java and Eclipse and there was possible to read in English the full stacktrace.. I whish it was as simle using XCode.

回答1:

You are probably getting one of those nasty EXC_BAD_ACCESS buggers. nope, no exceptions. For example

@try {

//        this is caught
//        NSMutableArray *ar = [NSMutableArray array];
//        [ar addObject:nil];

//        this is caught
//        NSMutableDictionary *dic = [NSMutableDictionary dictionary] ;
//        [dic setObject:@"a" forKey:nil];

//    this crashes , no exception
//    NSLog("21");

//    and this will cause a message sent to a dealloced instance (zombie).
//    no exception, just a bad crash.

    GEIntEffect *eff = [GEIntEffect intEffectWithString:@"*1.1"
                                              withOrder:geEffectOrderFightMagic
                                              andImpact:geEffectImpactPositive];
    [eff release];
    NSLog([eff description]);


    [[GameSpecs sharedGameSpecs] setupBattlesInitialSpecs];
    // with  a comment
}

@catch (NSException *e) {
    MPLOGERROR(@"*** an exception [%@] occured while writing the game specs, continuing.\n%@\n\n%@",
    e.description,
    e.callStackSymbols,
    e.callStackReturnAddresses);
}

the first two are trapped and the log statement is explicit, spewing me the stack trace in the log. The NSLog("21") ... ooops, forgot the "@" crashes badly. The fourth example, wrongly releasing an object before use ... well u know what that does. I can only suggest you :

  • check all your warnings (i had one on the bad NSLog)
  • Profile your app for zombies, using instruments. A message sent to a dealloced object will also crash nasty.
  • although there is a performance penalty, ie dont overdo it, some strategically placed try/catch blocks can help immensely. Typically, i wrap any 'update' method in Debug, removing the try/catch in Release.
  • recheck all your warnings :)

best of luck.

ps : here is what the log looks like when i trap an exception. The offsets are not to a 'line of code', symbolicating allows for that. But, typically, i have enough info from the trace to narrow it down to real close. :

-[MPGameSequencer sequenceInitState] : * an exception [* -[__NSArrayM insertObject:atIndex:]: object cannot be nil] occured while writing the game specs, continuing. (

0   CoreFoundation                      0x028de02e __exceptionPreprocess + 206
1   libobjc.A.dylib                     0x022bbe7e objc_exception_throw + 44
2   CoreFoundation                      0x02891b6a -[__NSArrayM insertObject:atIndex:] + 314
3   CoreFoundation                      0x02891a20 -[__NSArrayM addObject:] + 64
4   Battles                             0x00221c49 -[MPGameSequencer sequenceInitState] + 809
5   Battles                             0x0021e359 -[MPGameSequencer nextFrame:] + 121
6   Battles                             0x00067324 -[CCTimer update:] + 308
7   Battles                             0x00070444 -[CCScheduler update:] + 772
8   Battles                             0x0009dc81 -[CCDirectorIOS drawScene] + 225
9   Battles                             0x0009ef44 -[CCDirectorDisplayLink mainLoop:] + 52
10  QuartzCore                          0x007c32d2 _ZN2CA7Display11DisplayLink8dispatchEyy + 110
11  QuartzCore                          0x007c375f _ZN2CA7Display16TimerDisplayLink8callbackEP16__CFRunLoopTimerPv + 161
12  CoreFoundation                      0x0289d376 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
13  CoreFoundation                      0x0289ce06 __CFRunLoopDoTimer + 534
14  CoreFoundation                      0x02884a82 __CFRunLoopRun + 1810
15  CoreFoundation                      0x02883f44 CFRunLoopRunSpecific + 276
16  CoreFoundation                      0x02883e1b CFRunLoopRunInMode + 123
17  GraphicsServices                    0x035e57e3 GSEventRunModal + 88
18  GraphicsServices                    0x035e5668 GSEventRun + 104
19  UIKit                               0x00e9165c UIApplicationMain + 1211
20  Battles                             0x000c2bce main + 270
21  Battles                             0x00002a15 start + 53

)