![](/img/trans.png)
[英]ARC and dismissModalViewControllerAnimated : viewcontroller memory is NOT released
[英]iOS - ViewController not being released when popped under ARC
我有一個UITabBarController
作為我的主基本視圖控制器。 在第一個選項卡下,我有一個UINavigationController
,它當然有一個rootViewController
關聯的rootViewController
,稱之為vcA
。 vcA
有一個按鈕, vcB
使用以下代碼觸發子視圖控制器vcB
:
[self performSegueWithIdentifier:@"PlacesDetailsSegue" sender:senderDictionary];
這似乎有效,我在儀器中看到vcB的新分配正在發生。 當我將視圖控制器彈回到vcA
,一切看起來都有效,但似乎vcB永遠不會被釋放(即,永遠不會調用dealloc)。 所以每次從vcA->vcB->vcA->vcB
,內存使用量都會增加和增加。
我在vcB
有一些實例變量,所有這些都在dealloc
設置為nil
。 但是由於dealloc沒有被解雇,所以它們實際上從未被設置nil
。
我有一個[UIView animationWith...]
塊,它引用了self.view
的框架屬性,但我已經使用代碼管理了它:
__unsafe_unretained BBCategoryViewController *weakSelf = self;
[UIView animateWithDuration:duration
animations:^{
[_feedTableView setFrame:CGRectMake(0,
_navBar.frame.size.height,
weakSelf.view.frame.size.width,
weakSelf.view.frame.size.height - kMenuBarHeight)
];
}
completion:nil];
有誰知道我怎么能找到仍然保留在vcB pop上的對象?
作為參考,我的接口擴展是:
@interface BBCategoryViewController () <UITableViewDataSource, UITableViewDelegate> {
UITableView *_feedTableView;
UIRefreshControl *_refreshControl;
BBSearchBar *_searchBar;
MKMapView *_mapView;
BBNavigationBar *_navBar;
NSString *_title;
NSString *_categoryId;
NSArray *_feedArray;
}
@end
而我的dealloc(永遠不會被解雇)是:
-(void)dealloc {
NSLog(@"Dealloc: BBCategoryViewController\n");
_feedTableView = nil;
_refreshControl = nil;
_searchBar = nil;
_mapView = nil;
_navBar = nil;
_feedArray = nil;
}
更新:這實際上是由於子視圖中的保留周期,並且與我首先想到的並不直接與視圖控制器相關。 Dan F 在這里引導我找到正確的答案,萬一有人遇到類似的事情。
您的vcB應該完全取消分配,並且當您彈回vcA時調用dealloc。 你必須要么在某個地方保持對它的強烈引用,要么你的“流行”不正確。 如果你用segue這樣做,那可能是你的問題 - segues不應該用於倒退(除了展開segues)。 因此要么使用unwind segue,要么使用popViewControllerAnimated返回vcA。 此外,使用ARC時,無需在dealloc中將ivars設置為nil。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.