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

Memory management and NSUserDefaults?


  • Subject: Memory management and NSUserDefaults?
  • From: Mike Nowak <email@hidden>
  • Date: Sun, 6 Apr 2003 23:49:16 -0400

I am working my way through the Cocoa Recipes book (thanks Bill!) and I had a question on managing memory for user defaults (recipe 19). I noticed that on page 692, in step 6, one of the values from the defaults database is being retained and I got curious. Lets say I have a model object with instance variables and a setter for one of those variables does something like:

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

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

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

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?

Thanks in advance for any clarification. Forgive me if this has been covered before.

--
Mike Nowak
Health Media Research Laboratory, The University of Michigan
http://healthmedia.umich.edu/
_______________________________________________
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.

  • Follow-Ups:
    • Re: Memory management and NSUserDefaults?
      • From: Scott Anguish <email@hidden>
  • Prev by Date: New to ObjC/Cocoa - looking to get data from OS
  • Next by Date: Re: iChat-style table view
  • Previous by thread: Re: New to ObjC/Cocoa - looking to get data from OS
  • Next by thread: Re: Memory management and NSUserDefaults?
  • Index(es):
    • Date
    • Thread