Does @"some text" give an autoreleased or retain 1 object back?
Asked Answered
A

1

11

Given this code:

// Initialize string
NSString *name = @"Franzi";

@"" macro creates a NSString with given text (here the name Franzi) and a RETAIN COUNT OF 1?

So @"" gives an NSString with have to be released or not? Am I responsible for this object? Second code example then confuses me, even though I am using it that way:

NSSting *message;
message = [NSString stringWithFormat:@"Hello @%!",name];
//message = [NSString stringWithFormat:@"Hello Girl!"];

So message gets released in next run loop, k. But what is with the NSString given as argument for stringWithFormat?

Does the class object NSString release the NSString @"Hello %@"/@"Hello Girl" given as arguement? Or does @""-Konstruktor only give back autoreleased NSStrings?

Apennines answered 20/5, 2011 at 8:43 Comment(2)
A few other questions on Stack Overflow about constant/literal strings: 1 2 3 4 5Chondrite
@"" is not a macro, but an Objective-C string literal.Brodie
S
22

The NSString literal notation @"" gives you compile-time constant strings that reside in their own memory space and have constant addresses.

Contrary to popular belief, the reason why you don't release literal strings is not because they are part of the autorelease pool. They aren't — instead, they spend the entire application's lifetime in that same memory space they're allocated at compile time, and never get deallocated at runtime. They're only removed when the app process dies.

That said, the only time you need to memory-manage constant NSStrings is when you retain or copy them for yourself. In that case, you should release your retained or copied pointers, just like you do any other object.

Another thing: it's the literals themselves that don't need memory management. But if you pass them as arguments to NSString's convenience methods or initializers, like you do with stringWithFormat:, then it's those objects returned by the methods and initializers that follow all memory management rules normally.

Sebiferous answered 20/5, 2011 at 9:1 Comment(10)
So when my App is using to much memory, is it bad to use the @"" notation, or do all NSString come to this other memory space? And why do I release the NSString created with NSString, could I just skip that (for the object name above for example)?Apennines
I wouldn't blame overuse of strings for this as they have relatively low memory footprint. If you use stringWithFormat: to create strings, they are autoreleased. If you use any of the init methods to create strings, you need to release them yourself (or call autorelease).Sebiferous
So why release, if they don't really get deleted, because they stay in their own memory space?Apennines
@Virus42: If you make a string like NSString *a = @"a";, you never release a. But if you later do NSString *b = [[NSString alloc] initWithString:a];, you need to release b because b isn't a literal string. If you do NSString *c = [a retain];, you release c because you retained something. Whether it'll leak otherwise I'm not sure, but if you follow memory management rules you'll be fine. Balance every init/new/retain/copy with a release.Sebiferous
Thanks. So now I understand. Strings are literals like an int (I wouldn't release the number 1 either).Apennines
@Sebiferous You’re right in general, but it’s amusing that in your example above b is also a constant string, just like a. In fact, they’re the same string.Chondrite
@Virus42: To be very clear, only strings that you create using @"" are literals.Sebiferous
@Bavarious But the Documentation says (So it is copy): initWithString: Returns an NSString object initialized by copying the characters from another given string.Apennines
@Virus42 And that is indeed true in general, and you should manage memory just like BoltClock’s said. It just so happens that -initWithString: has a different behaviour when the argument is a constant string — instead of creating a new string, it returns the argument itself. It’s just an implementation detail that shouldn’t affect how you manage memory, but it’s interesting none the less.Chondrite
Just to be pedantic, it's actually the 'alloc', not the 'init', that you're balancing with a release. Also, the initWithString behaviour is the same for literal strings as it is for any NSString, it doesn't copy the actual characters, just the pointer.Enliven

© 2022 - 2024 — McMap. All rights reserved.