• 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: NSMutableDictionary, NSNumber and more memory management
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableDictionary, NSNumber and more memory management


  • Subject: Re: NSMutableDictionary, NSNumber and more memory management
  • From: Memo Akten <email@hidden>
  • Date: Thu, 28 Aug 2008 13:28:23 +0100

Thanks Joshua, that clears up the memory stuff... i think i finally get it now: unless you explicitly alloc or retain, you never need to release...
cheers,
memo.



On 28 Aug 2008, at 13:12, Joshua Pennington wrote:

On Aug 28, 2008, at 6:46 AM, Memo Akten <email@hidden> wrote:
q1:
Is this all correct? When you setObject, the docs say that the object receives a retain, so that means its safe (and correct) for me to do the above? Do I need to release the NSNumbers in dealloc? or does the NSMutableDictionary release them? I think its the latter but just wanted to double check.

numberWithUnsignedInteger: returns an object which you do not have ownership over and as a result, you are not responsible for releasing it. NSDictionary will retain it when setValue:forKey: is messaged.


q2:
this is my updating code:
- (void) updateValue:(NSString*)key {
id oldValue = [myDict objectForKey:key];
id newValue = [NSNumber numberWithUnsignedInteger:([oldValue unsignedIntegerValue] + 1)];
[myDict setObject:newValue forKey:key];
[oldValue release]; // do I need to release this?

objectForKey: returns an object over which you do not have ownership and as a result you do not have to release it after. When you call setValue:forKey: it retains newValue and releases it's reference to the original key.


Cheers,
Joshua
Sent from my iPhone

_______________________________________________

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: NSMutableDictionary, NSNumber and more memory management
      • From: Joshua Pennington <email@hidden>
References: 
 >NSMutableDictionary, NSNumber and more memory management (From: Memo Akten <email@hidden>)
 >Re: NSMutableDictionary, NSNumber and more memory management (From: Joshua Pennington <email@hidden>)

  • Prev by Date: Re: [meeting] Swindon, UK: upcoming cocoaheads
  • Next by Date: Re: NSMutableDictionary, NSNumber and more memory management
  • Previous by thread: Re: NSMutableDictionary, NSNumber and more memory management
  • Next by thread: Re: NSMutableDictionary, NSNumber and more memory management
  • Index(es):
    • Date
    • Thread