开发者

Why release a property that you've already set to nil?

Here are two methods in a view controller from an Apple tutorial:

- (void)viewDidUnload {
    self.eventsArray = nil;
    self.locationManager = nil;
    self.addButton = nil;
}

- (void)dealloc {
    [managedObjectContext release];
    [eventsArray release];
    [locationManager release];
    [addButton release];
    [super dealloc];
}

Couldn't the dealloc method be shortened to the following? If not, why not?

- (void)dealloc {
 开发者_StackOverflow社区   [managedObjectContext release];
    [super dealloc];
}


- (void)viewDidUnload is not guaranteed to be called, so you should always release things in dealloc too.

See this question to find out when it's called, and what you should do when it is.


No, because you cannot rely on viewDidUnload being called upon deallocation. viewDidUnload is only called when the view controller receives a memory warning while its view is not on screen. If the view controller gets deallocated, viewDidUnload is not called (AFAIK, I'm not entirely sure).


because it's a good practice to always cleanup your ivars in dealloc. something could go wrong, or you may encounter an execution you do not expect.


Setting eventsArray to nil just means it has no content, but still space for content Calling [eventsArray release] releases the space the array consumed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜