【发布时间】:2010-08-17 13:41:22
【问题描述】:
我是 Objective-c 的新手,目前正在开发一个小型应用程序。
我有一些内存管理问题,想问这个特定的问题。我有一个按钮,我正在改变它的外观:
for (UIView *subview in button.subviews)
{
if (![subview isKindOfClass:[UIImageView class]]) // don't remove UIImageView, otherwise it will not be able to set background
{
[subview removeFromSuperview];
}
}
[button setBackgroundImage: [[imageArray objectAtIndex:itemNumber] forState:UIControlStateNormal];
/*
creating myView and myLabel here
*/
[button addSubview:myView];
[button addSubview:myLabel];
[myLabel release];
[myView release];
代码实际上更复杂,但没有任何泄漏。尽管如此,每次我这样做时,分配的对象和使用的内存的数量都会不断增长。我该如何解决这个问题?
非常感谢
【问题讨论】:
-
看起来您的问题在于标签的创建。其余的看起来还不错……
-
会出现什么问题?我在代码中创建标签,从数组中为它分配一些文本(不要为它创建任何对象)然后释放标签。我认为在 [subview removeFromSuperview] 运行时将这些数据保存在内存中之后,我对此无能为力(((
标签: objective-c iphone memory-management memory-leaks