简体   繁体   中英

UIViewController not deallocated

I'm having this issue for some time and tried several ways to avoid it but now i've decided to fix it once and for all :)

I have a chat view controller which observe an NSSet with KVO, the ChatVC has a UITableView and every time a new ChatMessage being inserted to the NSSet, it's being added also as a UITableViewCell to the tableview.

Now, my issue is not with the observation, it's somehow related to memory management I guess. in my ChatVC the method dealloc never get called so that vc is always in the background observing that NSSet object.

I've tried manually remove the observer on viewWillDisappear but the ChatVC is still in the background.

Profiling it with Instruments showed me that actually it's coming from the pushViewController that lead to that ChatVC:

MZChatViewController* chatViewController = [[MZChatViewController alloc] initWithNibName:@"MZChatViewController" bundle:nil];
chatViewController.shouldShowIndetermineConnectingIndicator = showIndicator;

// instruments shows 100% on this row
[self.navigationController pushViewController:chatViewController animated:animated];

chatViewController = nil;

A little search here and google gave me actually nothing helpful, I've passed through all my code in that VC and really made an effort to find a retained cycle that can also cause this no deallocation problem and found nothing.

Solved: Apparently it was nothing to do with pushViewController:animated: method, Instruments just marked this spot as the cause for the retained cycle because it was firing an NSTimer which was running at viewDidLoad. So invalidating that timer in viewWillDisappear solved it.

Although this wasn't the problem here, perhaps this will help someone else who runs into a similar issue with deallocating UIViewControllers.

Our UIViewController wasn't deallocated because a custom delegate property held a strong reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM