I just started with iOS programming recently, and assumed that I understood Objective-C reference counting, but seems I didn’t… The following code runs completely but makes my app crash afterwards with EXC_BAD_ACCESS (code 1 or code 2).
I am not using ARC (PhoneGap-based project).
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");
Output is as I’d expect:
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
What’s the problem here? Is there some autorelease going on, or does the capped image keep a reference to the original? No idea.
imageNamedandresizableImageWithCapInsetsboth return autoreleased objects. You should not release them manually.In other words: if you did not
alloc,copyorretainit, you should notreleaseit!And as a last hint: forget about the
retainCountproperty, it is useless for you.