开发者

Memory Management in loops

A UINavigationController is pushing a UIViewController when clicking on an entry of a table. The appearing v开发者_如何学JAVAiew creates a lot of UILabels by a loop like this:

for(i=0, i < 50, i++)
{
    pointer = [[UILabel alloc] initWithFram:CGRectMake(i, 5, 5, 5)];
    pointer.text = ...;
    ...
    [scrollView addSubView:pointer];
    [pointer release];
}

How can i free all the memory when the view disappears? isn´t that a problem, that the pointer "pointer" points only to the last UILabel? and to the other UILabels there are no more pointer?

Because the views are pushed very slowly after having loaded some of them before.


You should make UILabel *pointer a local variable in this loop. To make it clear that you don't need the pointer somewhere else, because you don't.

You retain (alloc) them, the scrollView retains them, you release them. And when the scrollview gets released it will release the UILabels. All of them.

Everything is fine.


You don't need to free all the UILabels manually. You're already releasing them in a loop. So to handle memory properly just make sure that scrollView is released properly.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜