简体   繁体   中英

(iphone) uiimage, view, subviews memory management

I need to release uiimage/view/subviews when I want, and have a few questions regarding proper practice of releasing them.

  1. [imageView removeFromSuperview] would release the imageView and imageView.image?

  2. view = nil; would release its subviews/and associated uiimages recursively? if not, should I implement a recursive function to release a view's subviews?

Thank you


Edit.

I looked at UIView's library reference

addSubview --

This method retains view and sets its next responder to the receiver, which is its new superview.

removeFromSuperview --

If the receiver's superview is not nil, the superview releases the receiver. If you plan to reuse a view, be sure to retain it before calling this method and release it again later as appropriate.

still not sure [imageView release] releases uiImage associated with it, and would I still need recursive releasing of subviews. ie a view's getting dealloced would automatically guarantee it's subviews release?

When you do [imageView removeFromSuperview] , it won't release anything. You need to so [imageView release] afterward. Even so, you still need to put your memory releasing for that view in imageView 's dealloc.

[imageView removeFromSuperview] would release the imageView and imageView.image?

removeSuperView calls release on the view , but you should pay attention to the views retain count. Just because you called removeFromSuperview doesn't mean it's doing what you want.

view = nil; would release its subviews/and associated uiimages recursively? if not, should I implement a recursive function to release a view's subviews?

No, you probably want to do (depending on how you've managed your subviews during their creation. If the superview was their only reference, they likely have a retain count of 1 and therefore calling release after calling removeFromSuperview will result in an error):

for (UIView* subview in view){
    [subview removeFromSuperView];
    [subview release]
}
[view release];

EDIT: To answer your final question, no, calling release on a view does not automatically call release on all of its subviews. You have to do it yourself, whether with release or with removeFromSuperview.

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