iOS UIViewController,why "self" become to "wild pointer" in viewDidUnload?

165 views Asked by At

my code snippet:

- (void)viewDidUnload{
    [super viewDidUnload];
    self.statusView = nil;
    self.tableView = nil;
    self.noDataView = nil;
}

In a rare situation, my app crashed in line self.noDataView = nil;. When I debug by po self, it seemed that it's pointing something other than current controller. What is possible reason?

PS:self.tableView's delegate and dataSource is set to self in init method. Does that have any relation to this?

2

There are 2 answers

4
Sulthan On BEST ANSWER

First, [super viewDidUnload] should be used as the last statement. However, that won't fix your error, probably.

The reason for your problem is quite simple. Your controller is overreleased somewhere. Do you have zombie detection enabled? The code where the application crashes is usually irrelevant because the problem happened earlier.

6
Tarek Hallak On

viewWillUnload is deprecated now, you can't count on it anymore, any question about it will lead you to the below references.

From Apple:

In iOS 6, the viewWillUnload and viewDidUnload methods of UIViewController are now deprecated. If you were using these methods to release data, use the didReceiveMemoryWarning method instead. You can also use this method to release references to the view controller’s view if it is not being used. You would need to test that the view is not in a window before doing this.

And Quote WWDC 2012:

The method viewWillUnload and viewDidUnload. We're not going to call them anymore. I mean, there's kind of a cost-benifit equation and analysis that we went through. In the early days, there was a real performance need for us to ensure that on memory warnings we unloaded views. There was all kinds of graphics and backing stores and so forth that would also get unloaded. We now unload those independently of the view, so it isn't that big of a deal for us for those to be unloaded, and there were so many bugs where there would be pointers into.

Edit: For your problem in iOS 5.1, viewDidUnload is used to release anything you have made when the view was created, so unless you are creating or retaining objects it in viewDidLoad or your nib, you may not release them in viewDidUnload.