这是我的代码:关于自动释放/释放和[池释放]
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *string = [[NSString alloc] initWithFormat:@"s"];
[string autorelease];
NSLog(@"retainCount of string is %d", [string retainCount]);
[pool release];
NSLog(@"retainCount of string is %d", [string retainCount]);
当我试着去了解自动释放和释放,我很困惑。 如果使用[string autorelease],发送释放消息到pool后,字符串的retainCount仍然是1.但是用[string release]来代替[string autorelease],最后string的retainCount将是0.我对autorelease的了解是“通过发送一个autorelease消息将对象添加到当前的自动释放池中供以后发布”。为什么我向它发送一个autorelease消息并释放池,我仍然可以访问该对象。
retainCount永远不会返回零。发送解除分配的对象会产生未定义的行为。 – bbum 2012-04-02 15:21:23