开发者

Can we do [self release];?

开发者 https://www.devze.com 2023-04-05 14:17 出处:网络
It works... it doesn\'t crash, and it appears to do what I need. This could be a design flaw of my application but its the easiest way to do what I need. Basically a view is displayed and controls wi

It works... it doesn't crash, and it appears to do what I need.

This could be a design flaw of my application but its the easiest way to do what I need. Basically a view is displayed and controls within the object itself determine when it is removed from the superview (at which point it needs to be released).

At this point its easiest to do [self release], which appears to work.

However, I'm aware of object ownership, and I'm aware that instantiating an object which releases itself is not a good design of object ownership - ie the owner class (the one which instantiates the new object) should be the one which releases it. This would then, however, require me to write delegate methods calling back to the owner class, all just to release the object.

Thoughts please? :)

开发者_Python百科

Thanks


Basically a view is displayed and controls within the object itself determine when it is removed from the superview (at which point it needs to be released).

Usually, the superview has the retained reference to the child view, and when it gets removed, that reference is released. Why do you need to release it again?

If you do [self release] you are removing ownership of this object from some other piece of code (that holds a retained reference to it). If you do that, that other piece of code will still have a non-nil reference to a piece of garbage memory. It seems to me that this other piece of code should be notified of this situation (and then it might as well issue the release itself).

Once added to the superview I can just release?

The pattern you usually see in code samples is

UIView *view = [[UIView alloc] initWithFrame...];
[self addSubView:view];
[view release];


If you know what you're doing, and you're your own class' client, you're fine. About being a bad design: you're right. I'd suggest to notify the class that is instantiating the one you want to release that it's disappeared, so it can release it. You could use a protocol, or NSNotificationCenter.

0

精彩评论

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

关注公众号