Xcode 4:未记录到控制台的异常

时间:2011-07-28 14:29:20

标签: ios xcode xcode4

我最近升级到Xcode 4,还没有弄清楚如何将异常和错误消息记录到运行控制台。

示例 :在Xcode 3中,[[NSArray array] objectAtIndex:1]会导致以下内容被记录到控制台。

2011-08-10 10:27:22.061 App[28662:40b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 1 beyond bounds for empty array'
    *** Call stack at first throw:
(
    0   CoreFoundation                      0x015babe9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0170f5c2 objc_exception_throw + 47
    2   CoreFoundation                      0x015b080c -[__NSArrayI objectAtIndex:] + 236
    3   App                                 0x00002514 -[AppDelegate application:didFinishLaunchingWithOptions:] + 357
    4   UIKit                               0x003fc1fa -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    5   UIKit                               0x003fe55e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439
    6   UIKit                               0x00408db2 -[UIApplication handleEvent:withNewEvent:] + 1533
    7   UIKit                               0x00401202 -[UIApplication sendEvent:] + 71
    8   UIKit                               0x00406732 _UIApplicationHandleEvent + 7576
    9   GraphicsServices                    0x01c24a36 PurpleEventCallback + 1550
    10  CoreFoundation                      0x0159c064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    11  CoreFoundation                      0x014fc6f7 __CFRunLoopDoSource1 + 215
    12  CoreFoundation                      0x014f9983 __CFRunLoopRun + 979
    13  CoreFoundation                      0x014f9240 CFRunLoopRunSpecific + 208
    14  CoreFoundation                      0x014f9161 CFRunLoopRunInMode + 97
    15  UIKit                               0x003fdfa8 -[UIApplication _run] + 636
    16  UIKit                               0x0040a42e UIApplicationMain + 1160
    17  App                                 0x00002393 main + 85

此异常不会将任何记录到Xcode 4中的控制台。

我能够通过添加异常断点来查看调用堆栈 - 但是,继续超过异常断点不会将任何内容记录到控制台(甚至不是模糊的SIGABRTEXC_BAD_ACCESS消息)。

我在“编辑方案”窗口的“诊断”选项卡中选中了“日志例外”和“启用僵尸对象”,但它没有帮助。还有其他可能缺少的设置吗?

非常感谢。

5 个答案:

答案 0 :(得分:0)

请考虑使用以下内容增强您的gdbinit文件:

How do I set these break points in ~/.gdbinit?

fb -[NSException raise]
fb -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:]
fb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]

#define NSZombies
# this will give you help messages.  Set to NO to turn them off.
set env MallocHelp=YES
# might also be set in launch arguments.
set env NSZombieEnabled=YES
set env NSDeallocateZombies=NO
set env MallocCheckHeapEach=100000
set env MallocCheckHeapStart=100000
set env MallocScribble=YES
set env MallocGuardEdges=YES
set env MallocCheckHeapAbort=1

set env CFZombie 5

fb -[_NSZombie init]
fb -[_NSZombie retainCount]
fb -[_NSZombie retain]
fb -[_NSZombie release]
fb -[_NSZombie autorelease]
fb -[_NSZombie methodSignatureForSelector:]
fb -[_NSZombie respondsToSelector:]
fb -[_NSZombie forwardInvocation:]
fb -[_NSZombie class]
fb -[_NSZombie dealloc]

fb szone_error

正如Kendal在这篇原帖中的评论所述:如果你还没有gdbinit文件,你需要创建一个名为.gdbinit的新文件 - 将它放在你的主目录中并用提供的内容填充它。现在每次gdb启动它都会执行此文件中的命令。

希望这有帮助。

答案 1 :(得分:0)

在以下所有内容中,shareKit有内存泄漏!

我的所有代码都没有泄漏的迹象,但是分享到shareKit!

泄露的对象#地址大小负责的图书馆负责框架 NSMallocBlock ,1 0xf641c50 32字节Twitter - [TWInFlightSessionCallInfo setRemoteCall:] TWInFlightSessionCallInfo,1 0xf676380 16字节Twitter - [TWSession emptyCallInfo] Malloc 16字节,1 0xf6457c0 16字节Twitter - [TWSession recordAndIssueCallInfo:] Malloc 128字节,2<多个> 256字节libdispatch.dylib dispatch_queue_create $ VARIANT $ up NSLock,1 0xf64b450 64字节Twitter - [TWSession init] TWSession,1 0xf62c3e0 32字节Twitter - [TWTweetComposeViewController会话] NSMutableArray,1 0x2c8540 32字节Twitter - [TWSession init] NSMallocBlock ,1 0x1084ebb0 32字节Twitter - [TWInFlightSessionCallInfo setRemoteCall:]

答案 2 :(得分:0)

您可以通过在App Delegate中建立未捕获的异常处理程序来实现此目的。

void uncaughtExceptionHandler(NSException *exception)
{
   if ( exception )
   {
      NSLog(@"Exception %@", exception);
      NSLog(@"Exception Call Stack %@", [exception callStackSymbols]);
   }

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   ...

   NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);

答案 3 :(得分:0)

我在main.m中捕获异常这是我在最近的应用程序中编码的内容。我还在Edit Scheme中启用了Zombie对象,我也发现在我的应用程序中添加异常断点很有帮助。

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    int retVal;
    @autoreleasepool {
        @try 
        {
            retVal = UIApplicationMain(argc, argv, nil,        NSStringFromClass([AppDelegate class]));
           }
        @catch (NSException *exception) 
       {
           NSLog(@"CRASH===> %@", [exception callStackSymbols]);
            @throw;
        }  
       @finally 
       {

       }
       return retVal;
    }
}

答案 4 :(得分:-1)

我在XCode 4.6中尝试了你的例子,我得到了以下日志:
***由于未捕获的异常'NSRangeException'终止应用程序,原因:' * - [__ NSArrayI objectAtIndex:]:索引1超出空数组的边界' ***首先抛出调用堆栈:
(0x1cd0012 0x110de7e 0x1c85b44 0x4547 0x1121705 0x552c0 0x291a64 0x1121705 0x552c0 0x55258 0x116021 0x11657f 0x1156e8 0x84cef 0x84f02 0x62d4a 0x54698 0x1c2bdf9 0x1c2bad0 0x1c45bf5 0x1c45962 0x1c76bb6 0x1c75f44 0x1c75e1b 0x1c2a7e3 0x1c2a668 0x51ffc 0x285d 0x2785为0x1) libc ++ abi.dylib:terminate调用抛出异常