Crash in autorelease pool flush when NSWindow closed immediate after allocation

Originator:chris.bracken
Number:rdar://FB13291861 Date Originated:2023-10-20
Status:Open Resolved:
Product:AppKit Product Version:
Classification: Reproducible:
 
When an NSWindow is closed immediately after allocation, a crash occurs during objc_autoreleasePoolPop when the surrounding autorelease pool is flushed. 

Either of the following will avoid the crash:
1. Set window.releasedWhenClosed = NO;
2. Do not call close.

The following is a full repro: 
@autoreleasepool {
  NSWindow* window = [[NSWindow alloc] init];
  [window close];
}

For convenience, I've posted a project here: https://github.com/cbracken/NSWindowCrash that demonstrates the issue. 


## Expected behaviour
Closing an NSWindow immediately after allocation should succeed.


## Actual behaviour
Closing an NSWindow immediately after allocation, followed by being flushed from the autorelease pool causes a crash with the following stack trace.
Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libobjc.A.dylib               	       0x18d8f7ed8 objc_release + 16
1   libobjc.A.dylib               	       0x18d8ff418 AutoreleasePoolPage::releaseUntil(objc_object**) + 196
2   libobjc.A.dylib               	       0x18d8fbaf0 objc_autoreleasePoolPop + 260
3   NSWindowCrash                 	       0x102213f40 main + 96 (main.m:12)
4   dyld                          	       0x18d945058 start + 2224


## Discussion
While a developer is unlikely to ever intentionally write such code, it may occur in the context of a more complicated app where a window is both allocated and hits some condition that results in immediate close, or in minimal unit tests where a developer allocates a window in order to perform a test (in my case, related to accessibility notifications), and then clean up after itself.

Either way, this probably shouldn't crash, or should be documented in NSWindow close.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!