title: Why does this simple NSWindow creation code trigger an autorelease pool crash on shutdown under ARC?
body: <p>I am having an issue with an autorelease pool crash on shutdown which I've reduced to the small test case below that simply creates a window and then closes it. The crash disappears if the <code>-fobjc-arc</code> flag is taken away. Running on OS X 10.8.2, Clang 4.1 (421.11.66). I am hoping that someone with a more in depth understanding of ARC can enlighten me as to what is going on here - running with zombie objects on shows that it is the NSWindow object that is getting released too many times, or not retained enough, but I thought ARC was meant to take care of all this?</p>
  
  <p>The stack trace is:</p>
  
  <pre><code>0   libobjc.A.dylib                 0x00007fff8fad4f5e objc_release + 14
  1   libobjc.A.dylib                 0x00007fff8fad4230 (anonymous namespace)::AutoreleasePoolPage::pop(void*) + 464
  2   com.apple.CoreFoundation        0x00007fff99d22342 _CFAutoreleasePoolPop + 34
  3   com.apple.Foundation            0x00007fff936e84fa -[NSAutoreleasePool drain] + 154
  4   com.apple.Foundation            0x00007fff936effa0 _NSAppleEventManagerGenericHandler + 125
  5   com.apple.AE                    0x00007fff93a5ab48 aeDispatchAppleEvent(AEDesc const*, AEDesc*, unsigned int, unsigned char*) + 307
  6   com.apple.AE                    0x00007fff93a5a9a9 dispatchEventAndSendReply(AEDesc const*, AEDesc*) + 37
  7   com.apple.AE                    0x00007fff93a5a869 aeProcessAppleEvent + 318
  8   com.apple.HIToolbox             0x00007fff8d0c18e9 AEProcessAppleEvent + 100
  9   com.apple.AppKit                0x00007fff8e95c916 _DPSNextEvent + 1456
  10  com.apple.AppKit                0x00007fff8e95bed2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
  11  com.apple.AppKit                0x00007fff8e953283 -[NSApplication run] + 517
  12  Test                            0x00000001070e1d68 main + 152 (Test.mm:31)
  13  libdyld.dylib                   0x00007fff8e10c7e1 start + 1
  </code></pre>
  
  <p>And the code for the test case is:</p>
  
  <pre><code>// Tested with `clang++ -fobjc-arc -g Test.mm -framework Cocoa -o Test &amp;&amp; ./Test`
  
  #import &lt;Cocoa/Cocoa.h&gt;
  
  @interface MyApplication : NSApplication
  @end
  @implementation MyApplication
  - (void) applicationDidFinishLaunching: (NSNotification *) note
  {
      NSWindow * window = [[NSWindow alloc] initWithContentRect: NSMakeRect(100, 100, 100, 100)
                          styleMask: NSTitledWindowMask backing: NSBackingStoreBuffered defer: YES];
  
      [window close];
  
      [super stop: self];
  }
  @end
  
  int main()
  {
      @autoreleasepool
      {
          const ProcessSerialNumber psn = { 0, kCurrentProcess };
          TransformProcessType(&amp;psn, kProcessTransformToForegroundApplication);
          SetFrontProcess(&amp;psn);
  
          [MyApplication sharedApplication];
          [NSApp setDelegate: NSApp];
  
          [NSApp run];
      }
  
      return 0;
  }
  </code></pre>