views:

133

answers:

7
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.

+1  A: 

jupp, add autorelease to the end

Russo
+3  A: 

No it will not autorelease, you have to release it. If you used [NSString stringWithFormat], however, it would autorelease.

Seva Alekseyev
really? that's rather nice. typically not described anywhere I've seen either (and should be)
KevinDTimm
@kevindtimm it is described in the SDK documentation. Check it out sometime, all sorts of other goodies in there.
freespace
Um, I haven't seen this documented either, but lotsa experience says that class method [XXX XXXWithYYY] would normally return an autoreleasing object. Early on in my iPhone project, I encountered and debugged some overrelease bugs with those...
Seva Alekseyev
The convention is that methods beginning "alloc" or "new" or containing "copy" return an object you need to release; other method names return autoreleased objects. See http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html
David Gelhar
@Seva Alekseyev how did you miss it? Did you never read the SDK documentation? It is right there in the documentation for NSString: http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-SW13
freespace
@freespace: I've never seen a general statement about [Foo FooWithBar] documented. It's probably in the memory management guide somewhere...
Seva Alekseyev
@freespace - the term 'autorelease' is referenced in your link in 3 places, none of those are stringWithFormat - hence the 'surprise'
KevinDTimm
Ah, I see the confusion. It isn't that you didn't know about `stringWithFormat`. It is that you didn't know it returns an autorelease object. This is documented in http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html#//apple_ref/doc/uid/10000011 with a specific example at http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447 Hope that helps
freespace
bookmarked, thank you.
KevinDTimm
+1  A: 

yes, alloc == release (an easy rule to remember)

KevinDTimm
Also copy. And you have to watch for every instance of giving the ref away. I wish it was that simple...
Seva Alekseyev
yes, but I'm only answering the original question...
KevinDTimm
+1  A: 

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.

freespace
A: 

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.)

KennyTM
A: 

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"];
Steven Canfield
A: 

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, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. 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.]

jlehr
"instances of NSString created this way are not autoreleased" — wrong. `+stringWith…` is created using `[[[. alloc] init…] autorelease]`.
KennyTM
Doh! What I should have said was, it's better not to assume too much about the implementation since it's opaque and not guaranteed by contract.
jlehr