PDA

View Full Version : Memory Management with Static Fields




Lakario
Oct 31, 2008, 04:27 PM
In one of my application classes (subclass of TableCell) I create some static fields that are initialized for the whole class.


static UIImage *checkedImage = nil;

@implementation ItemCell

+(void)initialize {
checkedImage = [[UIImage imageNamed:@"checkbox_checked.png"] retain];
}


As far as memory management is concerned, where should these items be released? Is there a static dealloc?



SqueegyX
Oct 31, 2008, 04:48 PM
I don't think you really need to release those. When the app quits, any memory allocated to is immediately reclaimed. So for objects that need to exist for the entire life of your running application, I wouldn't worry about it.

Although, I would happy to know of a cleaner way from someone smarter than I.

Lakario
Oct 31, 2008, 06:43 PM
I don't think you really need to release those. When the app quits, any memory allocated to is immediately reclaimed. So for objects that need to exist for the entire life of your running application, I wouldn't worry about it.

Although, I would happy to know of a cleaner way from someone smarter than I.

This thread (http://discussions.apple.com/thread.jspa?threadID=1400350&tstart=8700) seems to essentially reiterate what you said. Thanks for the input. :)

PhoneyDeveloper
Oct 31, 2008, 10:05 PM
In general you don't release these kinds of objects.

However, you could treat it as a singleton. In that case it would be loaded lazily, not inside +initialize. And you could release it and nil the variable if you got a memory warning. It would be reloaded if it was needed after that.