title: Cocoa Application Crashing When Launched on Login
body: <p>I've found a bug in my application (SymSteam) which I'm having a particularly hard time fixing. It seems that whenever my application is launched as a login item it launches successfully but then crashes just a few seconds later. If the application is launched by the user (i.e. via Finder), it runs normally and doesn't crash. </p>
  
  <p>Checking Console immediately after logging in, I see the following:</p>
  
  <pre><code>04/08/2012 18:51:47.437 SymSteam[187]: SteamApps exists &amp; 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
  </code></pre>
  
  <p>The first two lines indicate that the my application started up fine and was able to register for the notifications it needed from NSWorkspace's notification centre. I can't figure out what's causing the segmentation fault though afterwards. </p>
  
  <p>There's also a crash report too. Here's an excerpt of it:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I'm baffled as to what's causing the problem since the application doesn't crash when not launched at login and even when launched at login, it launches but then crashes a few seconds later. I don't know how to go about debugging this bug either as I can't set breakpoints or use Instruments on the program since the bug only happens on login. </p>
  
  <p>Any advice on how to go about debugging this is greatly appreciated! </p>
  
  <p>For what it's worth, my application is a background application (so it doesn't have a dock icon or main window), it uses the <a href="http://growl.info" rel="nofollow">Growl</a> &amp; <a href="http://sparkle.andymatuschak.org" rel="nofollow">Sparkle</a> frameworks and it registers for notifications from NSWorkspace when it's launched. </p>
  
  <p><strong>Update:</strong></p>
  
  <p>Here's the code that's executed when my application is launched (some irrelevant code in the <code>applicationDidFinishLaunching:</code> method has been omitted). As I said above, this all gets executed before the crash. </p>
  
  <p>applicationDidFinishLaunching method:</p>
  
  <pre><code>- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
      [self.appController performInitialDriveScan];
      [self.appController startWatchingDrives];
  }
  </code></pre>
  
  <p><code>performInitialDriveScan:</code> simply uses NSFileManager a few times to see if certain directories exist. I won't post it here since it's quite long. I'm <em>pretty</em> confident it isn't the source of the crash. </p>
  
  <p><code>startWatchingDrives</code>:</p>
  
  <pre><code>- (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");
  }
  </code></pre>
  
  <p>This just adds an observer to NSWorkspace's notification centre that watches for drives being mounted/unmounted. </p>
  
  <p>That's everything that gets executed during the application's startup. The crash happens after the <code>startWatchingDrives</code> method is called and, as said above, only happens when the application is launched on login. It happens when my application isn't actually doing anything. </p>