2011-08-20 120 views
1

我這樣做:檢查保留計數

UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainback.jpg"]]; 

[self.view addSubview:backgroundImage]; 
NSLog(@" retain count1 : %d " , [backgroundImage retainCount]); 
[self.view sendSubviewToBack:backgroundImage]; 

[backgroundImage release]; 
NSLog(@" retain count2 : %d " , [backgroundImage retainCount]); 

我dealoc功能得到

retain count1 : 2 
retain count2 : 1 

1)我能得到這樣的消息:

- (void)dealloc{ 

NSLog(@" retain count2 : %d " , [backgroundImage retainCount]); 
[super dealloc]; 
} 

; 2)在最後我得到保留數對於backgroundimage所以它我s ok或應該是0(零) ??

謝謝..

+1

不,你不應該使用它。 爲我節省了解釋它檢查[這一點] [1] [1]:http://stackoverflow.com/questions/4636146/when-to-use-retaincount –

回答

3

按照Apple docs

的retainCount方法不佔任何未決的自動釋放發送到接收器 消息。

重要提示:此方法在調試內存 管理問題中通常沒有任何價值。因爲任何數量的框架對象可能有 保留一個對象以保存對它的引用,而在 同一時間自動釋放池可能在對象上保留任意數量的延遲 版本,這是非常不可能的有用的 來自此方法的信息。要了解必須遵守的內存管理的基本規則,請閱讀「內存管理 規則」。要診斷內存管理問題,請使用合適的工具: 甚至在運行程序之前,LLVM/Clang靜態分析器通常會發現內存管理問題 。 Instrument應用程序中的Object Alloc儀器 (請參見Instruments用戶指南)可以跟蹤 對象分配和銷燬。鯊魚(參見Shark用戶指南) 配置文件內存分配(在您的 程序的許多其他方面)。

+0

感謝harkonian .. !! :) – iUser

+0

沒問題。如果答案有幫助,請將其標記爲已接受。 – memmons