簡體   English   中英

為什么保留人數如此之高? 內存管理

[英]Why is the retain count so high? Memory Management

我一直在瀏覽我的應用程序,嘗試處理所有內存問題並閱讀內存管理。 我開始使用[object keepCount]跟蹤我的內存分配。 這是值得信賴的,因為我不斷發現計數真的很奇怪嗎?

有人可以解釋以下內容:

請記住,此應用程序委托和一個空的mainViewController沒有區別。 initWithRootViewController導致計數增加,但是我看不到添加一個的另一種方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      


/* Create the View Controllers */  
UIViewController *mainViewControl = [[[MainViewController alloc] init] autorelease];


/* Create the Navigation Controller */
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:mainViewControl] autorelease];


NSLog(@"retain count: %i",[mainViewControl retainCount]);

/* Set the toolbar to purple */
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
navigationController.navigationBar.tintColor = [UIColor colorWithRed:.6 green:.1 blue:.4 alpha:0.4];
navigationController.navigationBar.translucent = YES;

NSLog(@"retain count: %i",[mainViewControl retainCount]);
navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
navigationController.toolbar.tintColor = [UIColor colorWithRed:.6 green:.1 blue:.4 alpha:0.4];
navigationController.toolbar.translucent = YES;

[navigationController setNavigationBarHidden:YES animated:NO];
[navigationController setToolbarHidden:YES animated:NO];

NSLog(@"retain count: %i",[mainViewControl retainCount]);

[window addSubview:[navigationController view]];

NSLog(@"retain count: %i",[mainViewControl retainCount]);

這是日志〜

2011-01-17 19:47:21.278 ANA[5653:207] 3
2011-01-17 19:47:21.282 ANA[5653:207] 4
2011-01-17 19:47:21.286 ANA[5653:207] 7
2011-01-17 19:47:21.287 ANA[5653:207] 12
2011-01-17 19:47:21.301 ANA[5653:207] Load View

我不明白為什么更改這些屬性或引用navigationController導致保留計數激增。

我沒有自動發布就完成了它,也手動發布了,但是結果是一樣的。 基本上我不了解它,不知道keepCount命令是否可靠,因為如果我不理解這一點,我認為我無法調試其他地方的任何內存問題...

-retainCount官方文檔-retainCount

重要:在調試內存管理問題時,此方法通常沒有任何價值。 由於可能有許多框架對象保留了一個對象以保留對其的引用,而同時自動釋放池可能會在一個對象上保留任何數量的延遲發布,因此從此獲取有用信息的可能性很小方法。

不要依賴-retainCount 其他對象可能會在您不知道的情況下保留您的對象,而自動釋放的對象可能會給您對實際保留計數的錯誤印象。

您可以依靠Apple的框架對象來做正確的事情,並在適當的時候放棄對對象的所有權,並且需要確保自己也在做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM