【发布时间】:2015-03-01 06:00:53
【问题描述】:
在一个非常简单的测试应用程序中,我在 appdelegate 中有一个 NSViewController(强烈保留)。我将此 NSView 放在我的 NSWindow 的 contentView 中(我已在 Interface Builder 中将其设置为 Release on Close )。但是,当我退出应用程序时,NSView 的 dealloc 方法永远不会被调用。我本来希望它被以下流程调用 - NSWindow dealloc -> 删除内容视图 -> 删除所有子视图。 此外,除非我在 AppDelegate 的 applicationWouldTerminate 方法中将对其的强引用设置为 nil,否则不会释放 TestViewController。同样,我本来希望它会被释放。但是,看起来 AppDelegate 从未解除分配。 在我对 Objective-C 内存管理的理解中,我一定遗漏了一些基本的东西。可能是因为在 Mavericks Apple 强制退出应用程序,因此没有清理吗?我希望能在这方面指出正确的方向。谢谢
我的代码
#import "AppDelegate.h"
@interface TestView : NSView
@end
@implementation TestView
- (void)dealloc { NSLog(@"TestView - Dealloc"); }
@end
@interface TestViewController : NSViewController
@end
@implementation TestViewController
- (void)loadView { self.view = [[TestView alloc] init]; }
- (void)dealloc { NSLog(@"TestViewController - dealloc"); }
@end
@interface AppDelegate ()
@property (weak) IBOutlet NSWindow* window;
@property (strong) TestViewController* testViewController;
@end
@implementation AppDelegate
- (void)dealloc { NSLog(@"AppDelegate - dealloc"); }
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification
{
// Insert code here to initialize your application
TestViewController* testViewController = [[TestViewController alloc] init];
self.testViewController = testViewController;
[self.window.contentView addSubview:testViewController.view];
}
- (void)applicationWillTerminate:(NSNotification*)aNotification
{
// Insert code here to tear down your application
// self.testViewController = nil;
}
@end
【问题讨论】:
标签: objective-c cocoa