On Jan 21, 2009, at 9:23 AM, James Cicenia wrote:

Here is some code I wrote for the appdelegate to initialize some dictionaries and arrays: My main question has to do with memory and releasing it. In my app I will need listOfMonthNames for my popups and will also need dictionaryOfProduceTypes to populate other dependent popups.
...
-(void)initializeArraysAndDictionaries {
        if(!listOfMonthNames){
NSArray *listOfMonthNamesTmp = [[NSArray alloc] initWithObjects: @"January ",@"February ",@"March ",@"April ",@"May ",@"June ",@"July",@"August",@"September",@"October",@"November",@"December"];
                
                // enumerate over items
                printf( "----static array\n" );
                
                [listOfMonthNames 
arrayByAddingObjectsFromArray:listOfMonthNames2];
                
                // free memory
                [listOfMonthNamesTmp release];

First of all, you probably should just use the localized list of month names that you can get through NSDateFormatter:

        NSDateFormatter *aFormatter = [NSDateFormatter new];    
NSArray *listOfMonthNames = [[aFormatter standaloneMonthSymbols] retain];

This will give you an NSArray populated with NSString objects that contain the localized month names in the proper order. Notice that I retained the NSArray object. I did this because it is given to me autoreleased so if I want to own it I need to retain it. When I no longer need it I'll just call release on the object, this would most likely be done in the dealloc method of the class in which I initialized my object.

        if(!dictionaryOfProduceTypes){
// Create a distinct dictionary of types that hold an array of subtypes per type.
                // Types and subtypes are attributes of the ProduceItem object
                
                dictionaryOfProduceTypes = [[NSMutableDictionary alloc] init];
...

                [dictionaryOfProduceTypes release];
        }

By releasing the dictionaryOfProduceTypes object you are giving up control of the object and allowing it to be deallocated. This means that the object that you just initialized won't be around when you need it. You want to only call release when you are 100% sure that you won't need the object any more or when you are cleaning up, such as in the dealloc method for a class.

NSMutableArray *subtypes = [dictionaryOfProduceTypes objectForKey:item.type];
...

                        [subtypes release];

The objecForKey method returns an autoreleased NSArray. This means that its retain count is greater than zero for now, but it will be decremented to zero at some point in the future. By calling release on this object you are causing it to be over-released and even worse this will happen at some apparently random time in the future.

You should only call release on objects that you have created through methods that begin with alloc, new, or copy or objects that you have previously called retain upon. I suggest that you review the Cocoa Memory Management Rules to understand this further:

<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html >
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to