• 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: Garbage collection - was Beginner with Cocoa
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Garbage collection - was Beginner with Cocoa


  • Subject: Re: Garbage collection - was Beginner with Cocoa
  • From: Erik Buck <email@hidden>
  • Date: Tue, 25 Mar 2008 20:38:56 -0400

We have been asked not to paraphrase or restate the memory management rules in this forum because slight inaccuracies, imprecisions, misstatements, or just idiocy confuses people. Here is a quote:

From file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/chapter_3_section_5.html


If you create an object by allocating and initializing it (for example, [[MyClass alloc] init]), you own the object and are responsible for releasing it. This rule also applies if you use the NSObject convenience method new.
If you copy an object, you own the copied object and are responsible for releasing it.
If you retain an object, you have partial ownership of the object and must release it when you no longer need it.
Conversely,


If you receive an object from some other object, you do not own the object and should not release it. (There are a handful of exceptions to this rule, which are explicitly noted in the reference documentation.)
As with any set of rules, there are exceptions and “gotchas”:


If you create an object using a class factory method (such as the NSMutableArray arrayWithCapacity: method), assume that the object you receive has been autoreleased. You should not release the object yourself and should retain it if you want to keep it around.
To avoid cyclic references, a child object should never retain its parent. (A parent is the creator of the child or is an object holding the child as instance variable.)
Note: “Release” in the above guidelines means sending either a release message or an autorelease message to an object.


If you do not follow this ownership policy, two bad things are likely to happen in your Cocoa program. Because you did not release created, copied, or retained objects, your program is now leaking memory. Or your program crashes because you sent a message to an object that was deallocated out from under you. And here’s a further caveat: debugging these problems can be a time-consuming affair._______________________________________________

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


  • Prev by Date: Re: Best practice for overridden initializers in subclasses
  • Next by Date: Re: Best practice for overridden initializers in subclasses
  • Previous by thread: Re: Garbage collection - was Beginner with Cocoa
  • Next by thread: Core Data & Passwords
  • Index(es):
    • Date
    • Thread