debugTestLabel.text = [[NSString alloc] initWithFormat:@"%g, %g", @"Testing String", @"I am another"];
I alloc the String and immediately assign to the text, but I don't know whether the string can auto release or not.
debugTestLabel.text = [[NSString alloc] initWithFormat:@"%g, %g", @"Testing String", @"I am another"];
I alloc the String and immediately assign to the text, but I don't know whether the string can auto release or not.
No it will not autorelease, you have to release it. If you used [NSString stringWithFormat], however, it would autorelease.
If you read the documentation, you will find the text
property is copied - there is no need for you to keep that NSString
instance around.
So the answer is yes: you can (auto)release that NSString
instance.
Actually, it depends.
[[NSString alloc] initWithFormat:...];
This will construct a string with retain count of +1. If the text
property is @property(retain)
or @property(copy)
, then the debugTestLabel
will adopt its ownership, so you should -release
(or -autorelease
) it afterwards.
However, if the text
property is @property(assign)
, then the debugTestLabel
doesn't claim ownership, and you have to make sure the text lives longer than the label uses it.
(If the label is a UILabel
subclass then it's the first case.)
The rule is, if the method starts with init
you have to release
(or autorelease
it). Otherwise it's an autoreleased object (stringWithFormat:
for example). So in this case you should change the code to:
debugTestLabel.text = [[[NSString alloc] initWithFormat:@"%g, %g", @"Testing String", @"I am another"] autorelease];
or more standard,
debugTestLabel.text = [NSString stringWithFormat:@"%g, %g", @"Testing String", @"I am another"];
There are a lot of good responses here, though a few of the details could be slightly misleading. For example:
If you used [NSString stringWithFormat], however, it would autorelease.
In fact, though it may seem surprising, instances of NSString created this way are not autoreleased. However, this is correct conceptually in that your code is not responsible for the instances returned by +stringWithFormat:
and other +stringWith...
methods, so you can treat them as if they were autoreleased.
This will construct a string with retain count of +1.
Conceptually, yes, technically, no. The retain count in this case is actually -1
, since immutable NSString instances are cached by their class and never freed.
The rule is, if the method starts with init you have to release (or autorelease it).
Right idea, but actually, the rule is:
You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example,
alloc
,newObject
, ormutableCopy
), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own usingrelease
orautorelease
. Any other time you receive an object, you must not release it.
[From Memory Management Rules in Apple's Memory Management Programming Guide for Cocoa.]