开发者

When should I release an object in dealloc?

开发者 https://www.devze.com 2022-12-20 16:47 出处:网络
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 viewWil开发者_运维百科lDisappear if that view is a separat

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 viewWil开发者_运维百科lDisappear if that view is a separate rarely used part of your app.

Thanks.


Well first off, you probably want to release things in viewDidDisappear: (after they are off screen), not viewWillDisappear: In any event, you should probably release it in both viewDidDisappear: and dealloc ...

In viewDidDisappear: you should release anything you don't need while you are off screen in order to save memory. In dealloc you should release everything your object could possibly have retained. You want to do this regardless of whether you potentially released it in viewDidDisappear:, in case your code entered through a weird path where viewDidDisappear: 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.

0

精彩评论

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

关注公众号