下面的超级简单代码让我遇到了完全出乎意料的计时问题。其中一个变量是自动释放的,我不知道为什么。我没有使用 autorelease、KVO 等。它不应该发生。
被WindowController
设置为@property (retain)
'd of MainController
。
在-dealloc
中MainController
,我愿意self.windowController = nil;
但是,它一直等到自动释放池被刷新以释放 windowController。我希望在完成后立即调用 WindowController 的 dealloc self.windowController = nil
。即使我将 [mainController release] 包装在 NSAutoreleasePool 中,它仍然不会立即释放。
为什么会这样?
对于@property / NSWindowController,这似乎不是正确的行为。我错过了什么吗?
更正:这不是绑定。我正式不知道问题是什么。
主驱动:
[[MainController new] release];
主控制器.h:
#import <Foundation/Foundation.h>
#import "WindowControllerSubclass.h"
@interface MainController : NSObject {
WindowControllerSubclass *wc;
}
@property (retain) WindowControllerSubclass *wc;
@end
主控制器.m:
#import "MainController.h"
@implementation MainController
@synthesize wc;
- (id)init {
if (self = [super init]) {
// This is problem here >>> If I assign directly to wc, then it's not added to autorelease pool
self.wc = [[WindowControllerSubclass alloc] init];
[self.wc release]; // since it's (retain)'d
}
return self;
}
- (void) dealloc {
self.wc = nil;
NSLog(@"%@ deallocd (should be called after WC's dealloc)", [self className]);
}
@end
MainWindowControllerSubclass.h:
#import <Cocoa/Cocoa.h>
@interface WindowControllerSubclass : NSObject /* Not even NSWindowController */
@end
MainWindowControllerSubclass.m:
#import "WindowControllerSubclass.h"
@implementation WindowControllerSubclass
- (void) dealloc {
NSLog(@"%@ deallocd", [self className]);
}
@end