Search code examples
iphoneipadautoreleasensautoreleasepool

Autoreleasing twice an object


NSString *str = [[[[NSString alloc]init]autorelease]autorelease];
str = @"hii";
NSLog(@"%@",str);      

Can any one help me to tell about this code. Autoreleasing the object twice what will happened. When i run the code i didn't get any zombie. why it so.


Solution

  • The object gets released twice when the autorelease pool is destroyed, which is probably going to be at the end of the run loop iteration. Why it doesn't crash is, that NSString returns singletons for some instances, for example the empty string you create or string literals (you should NOT depend on it, thats just what currently happens!), these objects won't be deallocated and this is why you don't get a zombie.