开发者

Memory leak from static NSString

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 underst开发者_如何学Canding, 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜