我在我的应用程序 (SymSteam) 中发现了一个错误,我很难修复它。似乎每当我的应用程序作为登录项启动时,它都会成功启动,但几秒钟后就会崩溃。如果应用程序是由用户启动的(即通过 Finder),它会正常运行并且不会崩溃。
登录后立即检查控制台,我看到以下内容:
04/08/2012 18:51:47.437 SymSteam[187]: SteamApps exists & SteamAppsSymb exists, suggesting everything is A-OK.
04/08/2012 18:51:47.451 SymSteam[187]: Succesfully started observing everything I need to
04/08/2012 18:51:52.623 com.apple.launchd.peruser.501[124]: ([0x0-0x16016].com.simplecode.SymSteam[187]) Job appears to have crashed: Segmentation fault: 11
前两行表明我的应用程序启动良好并且能够从 NSWorkspace 的通知中心注册它需要的通知。我无法弄清楚是什么导致了分段错误。
还有一个崩溃报告。这是它的摘录:
OS Version: Mac OS X 10.8 (12A269)
Report Version: 10
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: EXC_I386_GPFLT
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libobjc.A.dylib 0x00007fff80902278 _cache_getImp + 4
1 libobjc.A.dylib 0x00007fff80903f51 lookUpMethod + 41
2 libobjc.A.dylib 0x00007fff80905d5e class_respondsToSelector + 31
3 com.apple.CoreFoundation 0x00007fff82adb7ab objectIsKindOfClass + 43
4 com.apple.CoreFoundation 0x00007fff82adb812 __exceptionMatch + 18
5 libobjc.A.dylib 0x00007fff8090d1d4 _objc_exception_do_catch(objc_typeinfo*, objc_typeinfo*, void**, unsigned int) + 144
6 libc++abi.dylib 0x00007fff81621807 get_adjusted_ptr(std::type_info const*, std::type_info const*, void**) + 71
7 libc++abi.dylib 0x00007fff81621439 __gxx_personality_v0 + 889
8 libunwind.dylib 0x00007fff8b678c22 _Unwind_RaiseException + 158
9 libc++abi.dylib 0x00007fff81621baa __cxa_rethrow + 72
10 libobjc.A.dylib 0x00007fff8090d5f5 objc_exception_rethrow + 40
11 com.apple.CoreFoundation 0x00007fff82a7ce36 CFRunLoopRunSpecific + 390
12 com.apple.HIToolbox 0x00007fff8c855774 RunCurrentEventLoopInMode + 209
13 com.apple.HIToolbox 0x00007fff8c855512 ReceiveNextEventCommon + 356
14 com.apple.HIToolbox 0x00007fff8c8553a3 BlockUntilNextEventMatchingListInMode + 62
15 com.apple.AppKit 0x00007fff87afefa3 _DPSNextEvent + 685
16 com.apple.AppKit 0x00007fff87afe862 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
17 com.apple.AppKit 0x00007fff87af5c03 -[NSApplication run] + 517
18 com.apple.AppKit 0x00007fff87a9a656 NSApplicationMain + 869
19 com.simplecode.SymSteam 0x0000000108df4d54 start + 52
Thread 1:
0 libsystem_kernel.dylib 0x00007fff82dad6d6 __workq_kernreturn + 10
1 libsystem_c.dylib 0x00007fff84c27f2c _pthread_workq_return + 25
2 libsystem_c.dylib 0x00007fff84c27cf3 _pthread_wqthread + 412
3 libsystem_c.dylib 0x00007fff84c121b1 start_wqthread + 13
Thread 2:: Dispatch queue: com.apple.libdispatch-manager
0 libsystem_kernel.dylib 0x00007fff82dadd16 kevent + 10
1 libdispatch.dylib 0x00007fff81bafe26 _dispatch_mgr_invoke + 883
2 libdispatch.dylib 0x00007fff81bafa2a _dispatch_mgr_thread + 54
Thread 3:
0 libsystem_kernel.dylib 0x00007fff82dad6d6 __workq_kernreturn + 10
1 libsystem_c.dylib 0x00007fff84c27f2c _pthread_workq_return + 25
2 libsystem_c.dylib 0x00007fff84c27cf3 _pthread_wqthread + 412
3 libsystem_c.dylib 0x00007fff84c121b1 start_wqthread + 13
Thread 4:
0 libsystem_kernel.dylib 0x00007fff82dad6d6 __workq_kernreturn + 10
1 libsystem_c.dylib 0x00007fff84c27f2c _pthread_workq_return + 25
2 libsystem_c.dylib 0x00007fff84c27cf3 _pthread_wqthread + 412
3 libsystem_c.dylib 0x00007fff84c121b1 start_wqthread + 13
Thread 0 crashed with X86 Thread State (64-bit):
rax: 0x1000000000000000 rbx: 0x00007fff70bf5810 rcx: 0x0000000000000001 rdx: 0x0000000000000000
rdi: 0x1000000000000000 rsi: 0x00007fff88323f93 rbp: 0x00007fff56e0b910 rsp: 0x00007fff56e0b8d0
r8: 0x0000000000000000 r9: 0x0000000000000000 r10: 0x00000000510558d1 r11: 0x1000000000000000
r12: 0x0000000000000000 r13: 0x0000000000000000 r14: 0x1000000000000000 r15: 0x00007fff88323f93
rip: 0x00007fff80902278 rfl: 0x0000000000010202 cr2: 0x00007fff88424000
Logical CPU: 0
我对导致问题的原因感到困惑,因为应用程序在登录时未启动时不会崩溃,即使在登录时启动时,它也会启动但几秒钟后崩溃。我不知道如何调试此错误,因为我无法设置断点或在程序上使用 Instruments,因为该错误仅在登录时发生。
非常感谢任何有关如何进行调试的建议!
值得一提的是,我的应用程序是一个后台应用程序(因此它没有停靠图标或主窗口),它使用Growl & Sparkle框架,并在启动时注册来自 NSWorkspace 的通知。
更新:
这是我的应用程序启动时执行的代码(applicationDidFinishLaunching:
方法中一些不相关的代码已被省略)。正如我上面所说,这一切都在崩溃之前执行。
applicationDidFinishLaunching 方法:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
[self.appController performInitialDriveScan];
[self.appController startWatchingDrives];
}
performInitialDriveScan:
只需使用 NSFileManager 几次来查看某些目录是否存在。我不会在这里发布它,因为它很长。我非常有信心这不是崩溃的根源。
startWatchingDrives
:
- (void)startWatchingDrives{
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self.saController selector:@selector(didMountDrive:) name:NSWorkspaceDidMountNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self.saController selector:@selector(didUnMountDrive:) name:NSWorkspaceDidUnmountNotification object:nil];
NSLog(@"Succesfully started observing everything I need to");
}
这只是向 NSWorkspace 的通知中心添加了一个观察者,用于监视正在安装/卸载的驱动器。
这就是在应用程序启动期间执行的所有内容。崩溃发生在startWatchingDrives
调用该方法之后,并且如上所述,仅在登录时启动应用程序时发生。当我的应用程序实际上没有做任何事情时,就会发生这种情况。