• 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: Memory management and NSUserDefaults?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory management and NSUserDefaults?


  • Subject: Re: Memory management and NSUserDefaults?
  • From: Scott Anguish <email@hidden>
  • Date: Mon, 7 Apr 2003 00:26:16 -0400

On Sunday, April 6, 2003, at 11:49 PM, Mike Nowak wrote:

But lets say that when the model was initialized, it got the value from NSUserDefaults:

myValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyValue"];

At this point myValue is not retained. If you want it to stick around.. you need to retain it.

Am I supposed to retain this new value? It seems like if my setter gets used and release this value that I didn't retain, it would cause memory problems. I'm guessing the rules for defaults are like those of collections and I shouldn't be releasing values in the defaults.

Should my initialization from the defaults database be more like:

myValue = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MyValue"] copy];

or

myValue = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MyValue"] retain];

instead of what I have above?


It depends. :-)

If you're using a setter method it would be a single place to handle retaining and releasing any new values. I'd suggest using it, doing a [self setMyValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"MyValue"]];

using a setter similar to what others might suggest...

[myValue release];
myValue = [newValue copy];

this is probably bad form... if myValue is the same as newValue, then you'll be releasing it before you've got a retain on it.

You could [myValue autorelease] instead of the release... or you could copy and then release the old myValue.

whether to use copy or retain depends on the situation... In this case, it's not as clear as in others.. If the original value that you're assigning to a variable can change, and you don't myValue to be the new value, then you should copy. This may not guarantee you a different object (if it's a non-mutable object, it is possible, and entirely legal for it to just do a retain).

Thanks in advance for any clarification. Forgive me if this has been covered before.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Memory management and NSUserDefaults? (From: Mike Nowak <email@hidden>)

  • Prev by Date: Re: iChat-style table view
  • Next by Date: Re: StripedTableView and sheets
  • Previous by thread: Memory management and NSUserDefaults?
  • Next by thread: workaround for broken numlock support?
  • Index(es):
    • Date
    • Thread