开发者

objective C (iphone) question about releasing

If I create a view, and add it as a subview and also add it to an array, do I have to release it twice?

    UIView* cat = [[UIView alloc] initWithFrame:someFrame];
    [self.vi开发者_如何学Cew addSubview:cat];
    [self.animals addObject:cat];
    [cat release];
    [cat release];

It just seems weird to me to have 2 release statements, and I haven't seen people doing that. But doesn't the retain count increase by 2 in this case?


You should only have one release — to balance the alloc. Neither addSubView: nor addObject: give the caller ownership over the object, so the caller does not need to balance them with a release. Reading the memory management guide should clear all this up for you.

It might help to remember "NARC" — if you call a method on an object that includes the word "new", "alloc", "retain" or "copy", you need to release it. As you can see in your code above, only alloc fits the bill. Since you only NARCed it once, you only need to release it once.


In addition to what Chuck wrote:

It's not that you release because addSubview: or addObject: might/might not increase the retain count.

That's completely wrong way to look at it. If self.view retains your object, it's self.view's responsibility to release it correctly, and that's nothing you should care about.

It's because you created the object by alloc you need to release it.


When the cat is added to self.view, then self.view owns the cat, and it is responsible to release the cat. It is the same as for self.animals. The self.animals owns the cat. When self.animals is released, it will release all objects it owned.

So, you should release cat only one. Because in your code block, you own only one cat, not two.


As has been mentioned you do not have to release it twice; once is all that is required. However you do not have to release it at all if you use autorelease:

UIView* cat = [[[UIView alloc] initWithFrame:someFrame] autorelease];
[self.view addSubview:cat];
[self.animals addObject:cat];

The cat object will be released toward the end of the RunLoop cycle. Using autorelease will make your code cleaner and more able to cope with change.

Also you do not need the self qualifiers on view and animals unless you want to distinguish them from locally scoped objects (it'd also be poor practice to have locally scoped variables named the same as your classes' members).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜