【发布时间】:2014-01-31 01:51:32
【问题描述】:
我在使用 NSWindowController 时遇到了一个奇怪的情况。我希望释放窗口控制器,但不会。它似乎没有符合我对 ARC 行为的期望。
我创建了一个简单的窗口控制器子类,PlainWindowController。它的接口和实现都是空的:
#import <Cocoa/Cocoa.h>
@interface PlainWindowController : NSWindowController
@end
@implementation PlainWindowController
@end
我用它创建了一个名为PlainWindowController.xib 的默认windowController xib,它有一个已经设置了委托和windowController 连接的窗口。
在一次测试中,我写了这段代码:
PlainWindowController *strongWindowController = [[PlainWindowController alloc] initWithWindowNibName:@"PlainWindowController"];
__weak PlainWindowController *weakWindowController = strongWindowController;
[strongWindowController showWindow:nil];
strongWindowController = nil;
STAssertNil(weakWindowController, @"The window controller should have been deleted, wasn't");
当这个测试运行时,弱引用不为零。
如果我省略了showWindow,那就是零。如果我使用init 而不是initWithWindowNibName,则为nil。
有人知道这里发生了什么吗?提前感谢您的任何指导。
【问题讨论】:
标签: objective-c macos cocoa automatic-ref-counting nswindowcontroller