這是我的代碼:關於自動釋放/釋放和[池釋放]
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