Is it necessary to set all @propertys with retain modifier declared as IBOutlet to nil inside the - (void)dealloc method? Will memory be consumed / wasted if I don’t do so?
Assume that Automatic Reference Counting is turned OFF.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your question seems to be “How do I handle a
retainproperty in dealloc?” Well let’s look at an example.When your view is loaded
myViewis retained by two objects, your view hierarchy (by an internaladdSubview:or similar) and your view controller (by means of the property setter method). A simple log is all that is needed to confirm this. placing this code inviewDidLoadwill confirm this with an output of 2.There are two methods where you should address this pointer, and both are automatically filled in for you by
Xcode.First in
viewDidUnload(Which is generally only called in low memory situations) you want to release the view, and since that will result in a dangling pointer you also want to set the pointer to nil. Xcode achieves this by simply using the setter method.The second place, and the place you asked about initially is
dealloc. In dealloc you similarly need to release your property although in this case (Main thread only execution, based on theIBOutletin the question) the dangling pointer should not be a problem. This seems reinforced by Xcode’s automatic implementation of dealloc.Of course setting the pointer to
nilin addition to releasing would not be detrimental at all. And may even be preferable if this were not a main thread onlyUIKitelement. But if you were worried about multi-threading you would likely just useatomicinstead.