• 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 question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: newbie memory question


  • Subject: Re: newbie memory question
  • From: "M. Uli Kusterer" <email@hidden>
  • Date: Mon, 1 Dec 2003 11:06:35 +0100

At 18:25 Uhr -0600 30.11.2003, Chris Hanson wrote:
The memory management rules are pretty clear on what all o the above is.

NSData *data = [[notice userInfo] objectForKey: NSFileHandleNotificationDataItem];

This gets a value from the dictionary. There's no use of +alloc or -copy so you have to assume you don't own the object and it's autoreleased. If you want to own it you have to retain or copy it.

Actually, this is *not* autoreleased. It is owned by the dictionary. Which means as soon as the dictionary goes away, this object may disappear as well. But if this is inside a notification and you get this passed in as a parameter, the caller won't dispose of the dictionary until your call has returned, which means in most cases it won't make a difference.

But, just to be overly correct: If a function doesn't contain "copy", "alloc", "load" or "new", the only thing you can assume is that the object isn't owned by you. If you want to keep it around for a longer time, retain it, and release it once you're done.

You can use it throughout your current function call or as long as the current autorelease pool exists, whichever is shorter, without retaining it. Cocoa gives no additional guarantees. Methods not containing "alloc", "copy", "load" or "new" may return shared instances (e.g. the font panel, NSNumber), they may return autoreleased objects (e.g. many factory/convenience methods), or they may simply give you pointers to objects owned by the receiver (e.g. NSDictionary's objectForKey: or many accessor methods).

The reverse applies as well: If you get objects from "alloc", "copy", "load" or "new", it is your responsibility to explicitly release/autorelease them when you don't need them anymore.

--
Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
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: newbie memory question
      • From: Chris Hanson <email@hidden>
  • Prev by Date: Re: NSColorWell and alpha values
  • Next by Date: Re: NSColorWell and alpha values
  • Previous by thread: Re: NSColorWell and alpha values
  • Next by thread: Re: newbie memory question
  • Index(es):
    • Date
    • Thread