我有以下问题:在一个执行流程中我使用 alloc,而在另一个流程中,不需要 alloc。 在 if 语句的末尾,无论如何,我释放了对象。 当我执行“构建和分析”时,出现错误:“调用者不拥有对象引用计数的不正确减少”。
如何解决?
UIImage *image;
int RandomIndex = arc4random() % 10;
if (RandomIndex<5)
{
image = [[UIImage alloc] initWithContentsOfFile:@"dd"];
}
else
{
image = [UIImage imageNamed:@"dd"];
}
UIImageView *imageLabel =[[UIImageView alloc] initWithImage:image];
[image release];
[imageLabel release];
最佳答案
您应该保留
第二种情况下的图像:
image = [[UIImage imageNamed:@"dd"] retain];
这样,从您的角度来看,两种可能的条件退出都会有一个引用计数为 1 的对象。
否则,您正在尝试释放
一个已经自动释放
的对象!
关于iphone - 引用计数的不正确减少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2993521/