Actually I am working on a project with ARC
enabled. I know using alloc
and init
is taking ownership
of the object. I know, If I create a string like this
NSString *myString = [[NSString alloc]initWithFormat:@"Something"];
then I need to release
the myString
on myself. What If I am using ARC enabled? I cannot release on myself. So will it create a leak? Or should I don't create object like this?
I can create a string like below code too.
NSString *myString = [NSString stringWithFormat:@"Something"];
But which type I need to use exactly for ARC enabled project ? What will happen if I use first type?
If you use ARC, all the necessary
release
calls will be added for you when you compile. It will not leak.The difference between
and
is that the first one will automatically released after the last reference to
myString
in that block, while the second one is an autoreleased instance that will only be released at the end of the run loop. It's not a big difference, but if you're using a lot of objects, try to avoid autoreleased ones to keep memory usage low.ARC takes care of the memory management, so no you don't need to worry about calling
release
on yourmyString
variable, ARC will do that for you. Also as a suggestion I would recommend using convenience methods to create your object such as[NSString stringWithFormat:@"Something"];
It's enough to set the string pointer to nil to release it.
You can also do the same things that you would be able to do without ARC, but with the advantage that if you don't explicitly do anything, the ARC will manage (almost) everything for you.
So to release it you set it to nil, let's see what else you could do:
UPDATE
Here's why sometimes you don't notice that an object is released:
In this case str=[[NSString alloc]initWithString: @"hey"] is equal to str=@"hey", with the difference that str is autoreleased and not released.But the compiler optimizes the code in str=@"hello", so if you are inside an autorelease block you won't have any problem, str2 will be printed correctly.
That's why I used initWithUTF8String, to avoid that compiler optimization.