• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Newbie Memory Questions on a Code Fragment
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie Memory Questions on a Code Fragment


  • Subject: Re: Newbie Memory Questions on a Code Fragment
  • From: Kenneth Bruno II <email@hidden>
  • Date: Wed, 21 Jan 2009 10:07:41 -0500

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 (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Newbie Memory Questions on a Code Fragment
      • From: mmalc Crawford <email@hidden>
    • Re: Newbie Memory Questions on a Code Fragment
      • From: James Cicenia <email@hidden>
References: 
 >Newbie Memory Questions on a Code Fragment (From: James Cicenia <email@hidden>)

  • Prev by Date: Newbie Memory Questions on a Code Fragment
  • Next by Date: Re: Format float value to display with commas
  • Previous by thread: Newbie Memory Questions on a Code Fragment
  • Next by thread: Re: Newbie Memory Questions on a Code Fragment
  • Index(es):
    • Date
    • Thread