Auto repeat object twice

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

Can anyone help me tell about this code. Automatize an object twice what happens. When I run the code, I did not get any zombies. why is this so.

+7
source share
2 answers

The object will be released twice when the autoresist pool is destroyed, which is likely to be at the end of the iteration of the launch cycle. Why doesn't it crash, is that NSString returns single lists for some instances, for example, the empty string you are creating, or string literals (you should NOT depend on it, that's what is happening at the moment!), These objects will not released, and that’s why you don’t get zombies.

+12
source

Firstly, there is no reason to call auto-advertising twice.

After the object is marked as an abstract, the repeated call of the auto advertisement on it will be simply ignored. See https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsautoreleasepool_Class/Reference/Reference.html

But in the above example, you are creating an empty string:

 NSString *str = [[[[NSString alloc]init]autorelease]autorelease]; 

Then you assign it another line:

 str = @"hii"; 

This means that the first line that you highlighted will simply be a leak, you made auto-advertisement so that it would be cleared at the end. But it makes no sense to highlight the line in the first place.

You can simply do:

 NSString *str =@ "hii"; NSLog(@"%@",str); 
+3
source

All Articles