is removeFromSuperview releases the object?

别来无恙 提交于 2020-01-01 07:20:13

问题


I am using removeFromSuperview for removing a view from its superview. I am also using release after removeFromSuperview on that object, sometimes its work fine but sometimes give bad access.

is removeFromSuperview also call release to the object ?


回答1:


Yes, removeFromSuperview also releases view.

You need to release your view after you have added it to its superview.

CGRect frame; // let's assume you already have defined frame and superview
UIView *superview;

UIView *subview = [[UIView alloc] initWithFrame:frame];
[superview addSubview:subview];
[subview release];

Or just use autorelease, when you create a view.

UIView *subview = [[[UIView alloc] initWithFrame:frame] autorelease];
[superview addSubview:subview];

In both cases, you don't need to call release after you call removeFromSuperview.




回答2:


Yes, addSubview calls retain, and removeFromSuperview calls release. So your call to release does not belong there (it probably belongs after you assign the subview, assuming you do not want to hold the object).




回答3:


The correct answer is "probably". This is an implementation detail that you shouldn't even really be thinking about. If your code needs to work with the subview, then you should be retaining it before you start working on it, and you should be releasing it when you're finished working with it (which may or may not coincide with removing it from it's superview).

I imagine testing would show that removeFromSuperview does release the view, but you cannot depend on this behaviour and should never assume it will happen. Even if it does always happen now it might not in future.

You shouldn't even need to think about memory management at such a low level as this. If you retain the object with retain or copy or alloc or new then you are responsible for counter-acting that with a release at some point in the future.

If you did not do any of these things, then you should not be releasing it. The methods for adding a subview to a view do not contain retain or copy in their name and therefore you are not supposed to release it yourself when you remove it from it's superview.

The view system is very complicated, especially on an iPhone with such a limited amount of RAM and not inadequate storage to use virtual memory. Stick to the rules and you should be fine. Or enable ARC.




回答4:


Actually removeFromSuperview decreases the retainCount property of the view Object and so as the release.

So,if you do the way Aleksejs said.

UIView *subview = [[UIView alloc] initWithFrame:frame];
[superview addSubview:subview];
[subview release];

subview -> alloc= retainCount = +1 ,

subview -> addSubView = retainCount = +1

subview -> release = retainCount = -1.

subview -> removeFromSuperView = retainCount = -1

(subview removed from memory)



来源:https://stackoverflow.com/questions/8366380/is-removefromsuperview-releases-the-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!