【发布时间】:2013-08-28 12:13:08
【问题描述】:
我的GameWindowController(NSWindowController 的子类)中有以下方法:
- (void)windowWillClose:(NSNotification *)notification {
AppDelegate *delegate = [NSApp delegate];
[delegate removeGameWindowController:self];
}
AppDelegate 中 removeGameWindowController 的代码为:
- (void)removeGameWindowController:(GameWindowController*)controller {
[self.controllers removeObject:controller];
}
self.controllers 是一个 NSMutableArray,其中包含我所有的 GameWindowControllers。
上面的代码似乎有一个竞争条件。当我关闭窗口时,它会随机崩溃EXC_BAD_ACCESS,如果我一次关闭所有窗口,几乎每次都会崩溃。
我的猜测是 ARC 在 removeGameWindowController: 返回之前或之后释放窗口控制器,使窗口带有一个指向控制器的悬空指针。我尝试添加controller.window.windowController = nil; 无济于事。
出于某种原因,使用(BOOL)windowShouldClose:(id)sender 中建议的委托方法代替https://stackoverflow.com/a/11782844/344544 可行,但不是可接受的解决方案,因为它不会在退出时调用。
如何在每个窗口关闭后可靠地从控制器数组中删除我的窗口控制器?是否有其他一些被调用的委托方法或一些 NSNotification 我可以在窗口完成关闭后订阅哪个火?
【问题讨论】:
标签: macos cocoa automatic-ref-counting