void test(void) {
@autoreleasepool {
NSDictionary *dict = copyDict();
NSLog(@"%lu", [dict retainCount]);
[dict release];
}
}
int main (int argc, const char * argv[])
{
test();
}
The above code is compiled without ARC and calls into a framework function - copyDict() that's compiled with ARC:
NSDictionary *copyDict(void) {
NSString *path = @"/var/root/dict_file";
NSDictionary *dict = @{
@"a":@"a"
};
[dict writeToFile:path atomically:YES];
return [[NSDictionary alloc] initWithContentsOfFile:path];
}
When I run it, it always runs into an EXC_BAD_ACCESS error here as the autorelease pool drains:
for: objc_autoreleasePoolPop
-> 0x104aa0058 <+88>: ldp x29, x30, [sp, #0x20]
0x104aa005c <+92>: add sp, sp, #0x30
0x104aa0060 <+96>: retab
I tried deleting [dict release] and that fixed it or deleting the autorelease pool.
However, I don't understand why I'm not supposed to release it manually. And as I understand it, the copyDict() function, while being compiled, would be compiled automatically by the clang compiler into returning a singly retained object cus I prefixed it with the copy keyword. Also, what baffles me is that the printed retain count is 2 instead of 1. Can someone help explain why this's happening?