I'm reading memory management rules to this point where it said
- (void)printHello {
NSString *string;
string = [[NSString alloc] initWithString:@"Hello"];
NSLog(@"%@", string);
[string release];
}
you have ownership and have to release string
, but I'm curious about the @"Hello"
. @" "
is the syntax for creating and NSString
, and it's an object. So doesn't that get leaked?
Bavarious's answer is correct. For the curious, I can add that this is documented in Apple's “String Programming Guide”, specifically the section “Creating Strings” where it says (emphasis mine):
@"…"
is a literal instance ofNSString
. When the compiler sees a literal string, it maps the string into the binary file (e.g. your program) and the string is available as anNSString
object when the binary is loaded (e.g. when you run your program). You don’t have to manage the memory occupied by literal strings because they’re an intrinsic part of your binary — they are always available, they never get released, and you don’t have to worry about managing their memory.