我最近刚开始使用 iOS 编程,并假设我理解了 Objective-C 引用计数,但似乎我没有......以下代码完全运行,但之后我的应用程序崩溃EXC_BAD_ACCESS
(代码 1 或代码 2)。
我没有使用 ARC(基于PhoneGap 的项目)。
for(int i = 0; i < 10; ++i)
{
UIImage *a = [UIImage imageNamed:@"NavigationBarBackButtonBlack.png"];
UIImage *b = [a resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 7)];
NSLog(@"a has %d retains, b has %d retains, a==b == %d\n", [a retainCount], [b retainCount], a==b ? 1 : 0);
[a release];
[b release];
NSLog(@"#%d\n", i);
}
NSLog(@"FINISHED\n");
输出如我所料:
a has 1 retains, b has 1 retains, a==b == 0
#0
a has 1 retains, b has 1 retains, a==b == 0
#1
a has 1 retains, b has 1 retains, a==b == 0
[...snip...]
a has 1 retains, b has 1 retains, a==b == 0
#9
FINISHED
这里有什么问题?是否有一些自动释放,或者加盖的图像是否保留对原始图像的引用?不知道。