2011-05-14 39 views
1

我只是跑在我的項目鐺靜態分析器在這一點上擁有的對象的引用計數錯誤減量,和我收到以下警告:CSA警告:不是由主叫方

Incorrect decrement of the reference count of an object that is not owned at this point by the caller

請你能告訴我我的問題是什麼。我通常可以很好地管理我的應用程序中使用的內存。

self.cupboardViewController = [[CupboardViewController alloc] initWithNibName:@"CupboardViewController" bundle:[NSBundle mainBundle]]; 
[self.window addSubview:self.cupboardViewController.view]; 

- (void)dealloc { 
    [[self cupboardViewController] release];//where I am getting the warning. 
    [super dealloc]; 
} 
+2

假設'cupboardViewController'標記爲'retain',無論如何你都會泄漏它,因爲它會自動保留你在示例第一行創建的新對象。您應該在將其分配給屬性之前自動釋放該對象,àla'self.cupboardViewController = [[[CupboardViewController alloc] init ...] autorelease];'。 – 2011-05-14 18:56:22

+0

我這樣做了,但它導致應用程序在一段時間後崩潰。 – 2011-05-14 19:11:22

回答

3

很確定你應該釋放實例變量,而不是屬性。

- (void)dealloc { 
    [cupboardViewController release]; 
    [super dealloc]; 
} 
0

如果你有cupboardViewController爲留置物然後在上面的代碼中設置self.cupboardViewController =創造的2

一個保留計數的問題是其對保留的,所以當你釋放它的dealloc中仍存在一個傑出的保留,因此它泄漏。

我用的代碼標準很簡單:

theProperty = [[NS* alloc] init]

當我的Alloc我的財產(創建一個單一的保留),那麼就:

[theProperty release];

在我dealloc方法。

這種方式我一致認爲,我沒有引用財產,只有iVar和迴避這些問題與保留和釋放。