Say I have a menu button called ‘Create window’ that creates a new window:
MyWindowClass * window = [MyWindowClass new];
In order to retain it, I add it to a mutable array (declared and synthesised as _articleArray = [NSMutableArray new];)
[_articleArray addObject:window]
This works great. If I include:
NSLog(@"Windows in mem: %lu",_articleArray.count);
The number increments each time that I click the button and another window appears on the screen.
Now, if I attach a selector to this ‘create window’ function to identify when the window closes:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowClosed:) name:NSWindowWillCloseNotification object:window];
This creates an error:
-(void) windowClosed:(NSNotification*)notification {
[_articleArray removeObject:[notification object]];
NSLog(@"Windows in mem: %lu",_articleArray.count);
The NSLog decrements when I close a window as expected, but as soon as the function ends, it throws an EXC_BAD_ACCESS error (code 13, address=0,0)
0x7fff97878710: movq 24(%rax), %rax
I am very confused. The number decrements, so I can only think that the function is working. So what is happening here?
EDIT: (lldb) thread backtrace
* thread #1: tid = 0x1c07, 0x00007fff97878710 libobjc.A.dylib`objc_msgSend_vtable13 + 16, stop reason = EXC_BAD_ACCESS (code=13, address=0x0)
frame #0: 0x00007fff97878710 libobjc.A.dylib`objc_msgSend_vtable13 + 16
frame #1: 0x00007fff97571503 Foundation`__NSFireTimer + 80
frame #2: 0x00007fff993a6da4 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
frame #3: 0x00007fff993a68bd CoreFoundation`__CFRunLoopDoTimer + 557
frame #4: 0x00007fff9938c099 CoreFoundation`__CFRunLoopRun + 1513
frame #5: 0x00007fff9938b6b2 CoreFoundation`CFRunLoopRunSpecific + 290
frame #6: 0x00007fff8df260a4 HIToolbox`RunCurrentEventLoopInMode + 209
frame #7: 0x00007fff8df25e42 HIToolbox`ReceiveNextEventCommon + 356
frame #8: 0x00007fff8df25cd3 HIToolbox`BlockUntilNextEventMatchingListInMode + 62
frame #9: 0x00007fff92ce3613 AppKit`_DPSNextEvent + 685
frame #10: 0x00007fff92ce2ed2 AppKit`-[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
frame #11: 0x00007fff92cda283 AppKit`-[NSApplication run] + 517
frame #12: 0x00007fff92c7ecb6 AppKit`NSApplicationMain + 869
frame #13: 0x0000000100006942 myApp`main + 34 at main.m:13
frame #14: 0x00007fff9094f7e1 libdyld.dylib`start + 1
This answer has been moved from the comments in the question at the request of the OP
You need to ensure that you remove your
NSWindowWillCloseNotificationobserver before the window is destroyed:And also ensure that the window has its
isReleasedWhenClosedproperty set toYESso that it cleans-up after itself when closed.