Re: resetting ivars safely
Re: resetting ivars safely
- Subject: Re: resetting ivars safely
- From: "Shawn Erickson" <email@hidden>
- Date: Thu, 13 Sep 2007 11:14:40 -0700
I should note that it doesn't seem to have clicked how simple the
memory management rules really are. It may help to just focus on the
simple rules that are stated on the following page.
<file:///Developer/ADC Reference Library/documentation/Cocoa/Conceptual/MemoryMgmt/index.html#//apple_ref/doc/uid/10000011i>
In particular the two I quoted below (these two are often all you need
to consider)...
---
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.)
---
Also... the only real difference between applying the "rules" to an
instance variable (ivar) and a local variable (lvar) is that you need
to consider life time / scope of that reference. Instance variables
exist for the life of the object that they are part of while local
variables only exist for the life of the block they are in.
- ivars need to follow the "rules" when assignments are made to them
and when the object, that the ivar is part of, is created/destroyed
(e.g. init and dealloc).
- lvars need to follow the "rules" when assignments are made to them
and when the block they are in comes into exists or goes away.
Finally make sure you understand that an object typed (NSArray*, id,
etc.) lvar or ivar only _points_ at an instance of object and the
object it points at is in no way tied to the pointers that point at
it. Related to this is the fact that a _pointer_ to an object isn't
useful unless it actually points at an object (for example ivars are
set to zero, aka nil, when the object they are part of is first
allocated). This is why in "init" methods you would normally create
the object instances that you need to have around during the life of
your object.
-Shawn
_______________________________________________
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