views:

131

answers:

2

I'm learning Objective - C and coming from a garbage collected world. I am creating a class (static) variable of a dictionary and I am unsure if I am doing it properly for memory management or not. I'm using a convenience method so the object should be auto-released, but I don't really know if I need to release or retain it in my class.

I can't find clear documentation on how class level objects are managed - any advice is appreciated. Thanks.

+(NSDictionary*) polygonNames{        
        NSDictionary* polygonNames = [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"Triangle", @"3",
                                      @"Square", @"4",
                                      @"Square", @"4",
                                      @"Pentagon", @"5",
                                      @"Hexagon", @"6",
                                      @"Heptagon", @"7",
                                      @"Octagon", @"8",
                                      @"Nonagon", @"9",
                                      @"Decagon", @"10",
                                      @"Hendecagon", @"11",
                                      @"Dodecagon", @"12",
                                      nil];
        return polygonNames;

    }
+3  A: 

If your data structure is immutable and isn't going to change, you can use a static variable, like so:

+ (NSDictionary *) polygonNames
{
    static NSDictionary *polygonNames = nil;
    if (!polygonNames) polygonNames = [[NSDictionary alloc] initWithObjectsAndKeys:/* objects and keys */];
    return polygonNames;
}
mipadi
Thanks - this is helpful. What I'm concerned about is the 'life' of this. Considering that it is static across all instances do I need to ever release it or is it sufficient that it is 'released' at the end of the program. I'm assuming it is created the first time any instance of the class is requested?Thanks again - b
WillyCornbread
It will stay around until the app terminates, which is fine — memory leaks are only big problems (per se) if you're not releasing something which is allocated frequently. For singletons (something that exists only once in memory) it's actually fairly common not to release them, and to rely on the system to release the small amount of memory it uses.To answer your second question, this allocates the value on first request. If you're using threads, you may want to look here: http://alanquatermain.net/post/114613488/lockless-lazily-initialized-static-global-variables
Jim Dovey
Thanks muchly, these answers were very helpful.
WillyCornbread
+2  A: 

I can recommend the site CocoaDev.com. On which you will find the following rule of thumb:

  • If you alloc, retain, or copy it, it's your job to release it. Otherwise it isn't.
  • If you alloc, retain, or copy it, it's your job to release it. Otherwise it isn't. Yes: read this again!

Search for "rules of thumb". As I'm not allowed to add hyperlinks..

Basically all class methods named dictionary*, array* and so on will return an auto-released object which you don't have to retain/release.

For further reading I can recommend the "memory management" page.

rastersize