Sometimes when coding in Objective C for the iPhone I wonder if I should release an object in dealloc or is it sometimes better to release in viewWillDisappear if that view is a separate rarely used part of your app.
Thanks.
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.
Well first off, you probably want to release things in
viewDidDisappear:(after they are off screen), notviewWillDisappear:In any event, you should probably release it in bothviewDidDisappear:anddealloc…In
viewDidDisappear:you should release anything you don’t need while you are off screen in order to save memory. Indeallocyou should release everything your object could possibly have retained. You want to do this regardless of whether you potentially released it inviewDidDisappear:, in case your code entered through a weird path whereviewDidDisappear:is not called. That might happen in the future when you reuse the view controller in a different context, or when Apple changes something about the collection view controllers you are housing your VC in.So long as you are appropriately nil-ing out the ivars after you release them (which should be automatically happening if you are using properties) then over releasing will not be an issue because if you release it in both places the second release will end up sending the release message to nil.