Re: Newbie OOP style question
Re: Newbie OOP style question
- Subject: Re: Newbie OOP style question
- From: mmalcolm crawford <email@hidden>
- Date: Fri, 12 May 2006 13:16:41 -0700
On May 12, 2006, at 12:38 PM, Pierce T. Wetter III wrote:
There are list guidelines about reformulating the rules? That's
pretty wack.
Yes, precisely because people keep misstating them, which causes more
unnecessary list traffic and confusion. There have been several
errors in this thread alone...
(For example:
"Pretty much any method that returns an object returns an
autoreleased object."
This is unlikely to be true.
"The exceptions for methods that return retained objects are small:
init, mutableCopy."
This list is incomplete.
"2. Did you get the object from someone else and you need to keep it
around? better call [retain]."
This is not necessarily the appropriate thing to do.)
All of these are given correct treatment in the documentation.
Though in this internet age, you have to watch out because things
you say may come back to bite you. Here's Mmalcom's version of the
rules:
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
It's not clear how this "bites" me (although that is not an issue for
discussion on this list). That article was written long before the
list guidelines (actually considerably before the date given in the
article) and before some of the existing documentation. Moreover,
it's complete, accurate, and underwent significant peer review at the
time. It's also -- along with others -- referred to indirectly in
the list guidelines:
<http://lists.apple.com/archives/cocoa-dev/2006/May/msg00001.html>
'"UNOFFICIAL Cocoa-dev Frequently Asked Questions"
<http://www.alastairs-place.net/cocoa/faq.txt>
Especially useful for pointers to documents relating to memory
management and book recommendations'
Personally, I vote for my version of the rules
For the purposes of this list, please do not.
as I think the official formulation could be clearer. It's the
number one complaint the newbies have.
If you think the documentation can be improved, please submit an
enhancement request.
Note, though, that the memory management document is updated in the
forthcoming release (see summary below).
Also, technically the rules are wrong, its not just instance
variables where you need to retain objects, its any time you need
to keep an object around.
See the next line: "Use retain and autorelease when needed to prevent
an object from being invalidated as a normal side-effect of a message."
Note also that the rules are given as a summary; developers are
expected to read the whole document for a complete understanding.
Also I notice that both you and I mention "dealloc", which the
official rules don't, but that's where a big chunk of "release"
calls have to live.
dealloc is does not in and of itself relevant to the fundamental
rules. Its role is, however, discussed in the complete document.
mmalc
Memory Management Rules
This document summarizes the rules for memory management in Objective-C.
This is the fundamental rule:
You take ownership of an object if you create it using a method whose
name begins with “alloc” or “new” or contains “copy” (for example,
alloc, newObject, or mutableCopy), or if you send it a retain
message. You are responsible for relinquishing ownership of objects
you own using release or autorelease. Any other time you receive an
object, you must not release it.
The following rules derive from the fundamental rule, or cope with
edge cases:
* As a corollary of the fundamental rule, if you need to store a
received object as a property in an instance variable, you must
retain or copy it. (This is not true for weak references, described
at “Weak References to Objects”, but these are typically rare.)
* A received object is normally guaranteed to remain valid within the
method it was received in (exceptions include multithreaded
applications and some Distributed Objects situations, although you
must also take care if you modify the object from which you received
the object). That method may also safely return the object to its
invoker.
* Use retain and release or autorelease when needed to prevent an
object from being invalidated as a normal side-effect of a message
(see “Validity of Shared Objects”).
* autorelease just means “send a release message later” (for some
definition of later—see “Autorelease Pools”).
For a more complete discussion of memory management in Objective-C
see “Object Ownership and Disposal”.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden