• 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: Confused about Memory Management with Collections
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Confused about Memory Management with Collections


  • Subject: Re: Confused about Memory Management with Collections
  • From: Dave DeLong <email@hidden>
  • Date: Sat, 28 Nov 2009 14:27:15 -0700

Hi Mazen,

Yes, objects that are added to collections objects (array, set, dictionary) are automatically retained by that collection and not released until removed from the collection or the collection is deallocated.  If you don't need them "anywhere else outside the dictionary", then you should release them only if you own them (ie, you retained, alloc'd, new'd, create'd, etc).

Since you created the dictionary with a convenience method, it is autoreleased and you do not need to release it yourself.  It will be released when the autorelease pool to which it belongs gets drained.

This document contains everything you need to know:  http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html  Additionally, if you have questions about a particular method, the documentation for that method will usually indicate how it works on that object.  For example, NSMutableDictionary's setObject:forKey: method: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/Reference/Reference.html#//apple_ref/doc/uid/20000141-BABBDAFD

HTH!

Dave


On Nov 28, 2009, at 2:19 PM, Mazen M. Abdel-Rahman wrote:

> Hi All,
>
> I am trying to find out how memory is retained/released when using different Cocoa collection classes.  Is there a good resource on this anyone can connect me to?
>
> Here is some detailed questions about what I am trying to find out:
>
> If I create a dictionary in the following manner (where myArray is an NSArray* and authorName is an NSString*)
>
> NSDictionary * myDict = [NSDictionary dictionaryWithObjectsAndKeys:myArray, @"Titles", authorName, @"Name",nil];
>
> Does this call automatically increase the retain count of myArray and authorName?  So that I should call release on them if I don't need to have them anywhere else outside the dictionary?  Or will calling release on them mess up the dictionary?
>
> Also - once I am done with this dictionary -  since I created it using a class method (and not alloc) - am I right in assuming that I should not call release on it?  And if that is the case when does it get released?
>
> Thanks for your help!
> Mazen Abdel-Rahman
> _______________________________________________
>
> 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

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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

References: 
 >Confused about Memory Management with Collections (From: "Mazen M. Abdel-Rahman" <email@hidden>)

  • Prev by Date: Confused about Memory Management with Collections
  • Next by Date: When to use exception handling?
  • Previous by thread: Confused about Memory Management with Collections
  • Next by thread: When to use exception handling?
  • Index(es):
    • Date
    • Thread