objective-c - @property(保留)自动释放还是释放对象?

标签 objective-c cocoa xcode4 nsautoreleasepool nswindowcontroller

我在使用下面的 super 简单代码时遇到了完全意想不到的计时问题。其中一个变量正在自动释放,我不知道为什么。我没有使用 autorelease、KVO 等。它不应该发生。

WindowController 设置为 MainController@property (retain)

MainController-dealloc 中,我执行 self.windowController = nil;

但是,它会一直等待,直到自动释放池被刷新以释放 windowController。我希望在 self.windowController = nil 完成后立即调用 WindowController 的 dealloc 。即使我将 [mainController release] 包装在 NSAutoreleasePool 中,它仍然不会立即释放。

为什么会发生这种情况?

<小时/>

这对于 @property/NSWindowController 来说似乎不是正确的行为。我错过了什么吗?

<小时/>

更正:它不是绑定(bind)。我正式不知道问题是什么。

主要驱动力:

[[MainController new] release];

MainController.h:

#import <Foundation/Foundation.h>
#import "WindowControllerSubclass.h"
@interface MainController : NSObject {
    WindowControllerSubclass *wc;
}

@property (retain) WindowControllerSubclass *wc;

@end

MainController.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

最佳答案

这没什么奇怪的,特别是如果你的NSWindowController位于自动释放池中。

当拥有该对象的每个对象都释放该对象(例如 x )时,该对象就会被释放。 Autorelease是延迟释放,即直到自动释放池耗尽后才真正释放。

考虑以下事件链:

    B creates x
    A owns x
    A autoreleases x. // x is not released; it's put on an autorelease pool
    B releases x.     // x is not dealloced yet, because x is not released by the autorelease pool
    autorelease pool is drained. x is sent another release message. nobody owns x. x is dealloc'd. 

这就是你所看到的。

--- 更新 ---

更准确地说,自动释放池的神秘用法源于您的行

[self.wc release]; 

这使用 wc 的 getter ,即它调用 [self wc] 。现在,默认的合成 getter 在 this portion of obj-c runtime 中实现。 ,特别是objc_getProperty_non_gc 。请注意,您的属性(property)是 (retain) ,即 (atmomic retain) 。为了保证原子性,getter retain s ivar,然后在 autorelease 之后返回它'ing它:

id *slot = (id*) ((char*)self + offset);
if (!atomic) return *slot;

// Atomic retain release world
spin_lock_t *slotlock = &PropertyLocks[GOODHASH(slot)];
_spin_lock(slotlock);
id value = objc_retain(*slot);
_spin_unlock(slotlock);

// for performance, we (safely) issue the autorelease OUTSIDE of the spinlock.
return objc_autoreleaseReturnValue(value);

这就是它被放入自动释放池的原因。无论如何,

obj.property=[[SomeClass alloc] init];
[obj.property release];

是个坏主意。在你的情况下,self.ivar第二行返回您在第一行中分配的内容,但在聪明的非综合访问器或多线程环境中不能保证这一点。当你这样做时

obj.property=x;
id y=obj.property;

xy可以不同,如果 obj进行一些巧妙的缓存,或者是否有另一个线程访问 obj哪些变化obj.property两条线之间。因此,请使用临时变量:

SomeClass* a=[[SomeClass alloc] init];
obj.property=a;
[a release];

关于objective-c - @property(保留)自动释放还是释放对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7061246/

相关文章:

Cocoa 基于文档的应用程序 : Notification not always received by observer

ios - UIApplication 如何知道何时在应用程序中创建 UIWindow?

cocoa - 包和 NSFileManager enumeratorAtURL :includingPropertiesForKeys:options:errorHandler:

iphone - 如何将 UISwitch 的状态保存到文件

cocoa - 通过 stringByEvaluatingJavaScriptFromString 将文本从文件传递到 webview :

iphone - iOS线图?

ios - 如何使用 Storyboard iOS 来布局单个 View 的纵向和横向方向?

version-control - 在xcode4中提交代码提交的键盘快捷键?

ios - SpriteKit : How to animate drawing of a rounded rectangle

iphone - 在 iOS 上查看大型 PDF 的最佳方式是什么?