Objective c NSView/NSViewController内存管理
在一个非常简单的测试应用程序中,appdelegate中有一个NSViewController(强烈保留)。我将此NSView放在我的NSWindow的contentView中(我已设置为在Interface Builder中关闭时发布)。但是,当我退出应用程序时,从未调用NSView的dealoc方法。我希望它会被以下流调用-NSWindow dealloc->removes content view->removes all subview。 此外,TestViewController不会解除分配,除非我在AppDelegate的applicationWouldTerminate方法中将对它的强引用设置为nil。再说一次,我本以为它会被解除分配。但是,AppDelegate似乎从未解除分配。 在我对Objective-C内存管理的理解中,我肯定缺少一些基本的东西。这可能是因为在Mavericks上,苹果强制退出应用程序,因此没有清理?如果能为我指明正确的方向,我将不胜感激。谢谢 我的代码Objective c NSView/NSViewController内存管理,objective-c,cocoa,Objective C,Cocoa,在一个非常简单的测试应用程序中,appdelegate中有一个NSViewController(强烈保留)。我将此NSView放在我的NSWindow的contentView中(我已设置为在Interface Builder中关闭时发布)。但是,当我退出应用程序时,从未调用NSView的dealoc方法。我希望它会被以下流调用-NSWindow dealloc->removes content view->removes all subview。 此外,TestViewController不会解
#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
应用程序终止并不需要清理所有单独的对象。当进程终止时,OSX内核将简单地回收进程使用的所有资源。这要快得多 从: 当应用程序终止时,可能不会向对象发送
dealloc
消息。由于进程的内存在退出时会自动清除,因此简单地允许操作系统清理资源比调用所有内存管理方法更有效
如果您确实需要在应用程序终止之前完成一些工作,请将其放入应用程序委托的-applicationWillTerminate:
方法中,或者观察应用程序对象发布的NSApplicationWillTerminateNotification
通知。此外,您不需要选择加入,或者,如果您的应用程序通常选择加入,则暂时禁用它,只要它在终止时确实需要执行某些操作