简体   繁体   中英

Why would you set-to-nil AND release a property of a UIViewController?

There's something I don't get in Apple documentation. Here's an extract from the -(void)viewDidUnload of the UIViewController class :

your dealloc method should release each object but should also set the reference to that object to nil before calling super.

If you use a retain and a synthetize for a xxx attribute in your code, why do most of Apple examples do a set-to-nil in viewDidUnload :

self.xxx = nil;

but recommands to do both a set-to-nil AND a release in the dealloc :

[xxx release];
self.xxx = nil;

Why is the set-to-nil not enough for the dealloc ?

ps : i know my question is very similar to this one "Why release a property that you've already set to nil?" but it's not strictly the same

[xxx release];
self.xxx = nil;

This is wrong as xxx will get released twice, recommended way is release iVar and set it to nil, not using property:

[xxx release];
xxx = nil;

The reason for not just using

self.xxx = nil;

is that calling setter method may have some side effects that may cause problems in dealloc method (ie use some other iVar that may be already deallocated)

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