Reputation: 20115
I'm a first time Objective C programmer. I've been reading other people's code and I often see static strings created but never released. Take this for example:
- (UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSSSTring* foo = @"foo";
// [code to return a cell for the table]
}
To my understanding, space for 3 characters in the heap has been allocated to store the string "foo". When the program terminates, those 3 characters are never reclaimed because the author never releases them. Isn't there a memory leak here? Why or why not?
Upvotes: 1
Views: 263
Reputation: 31053
Actually, constant strings like @"foo"
are treated specially by the compiler. In particular, they are not heap allocated, and they do not participate in reference counting, i.e., they are never actually released; their memory is part of your program's image, just like the content of, say, "foo"
. However, this should be treated as implementation detail of this particular kind of NSString
subclass. Follow the usual rules for reference retention/release.
Upvotes: 1