I think I get it
I think I get it
- Subject: I think I get it
- From: Danny Swarzman <email@hidden>
- Date: Mon, 5 May 2003 02:07:20 -0700
Thanks for all who responded to my message about memory.
I think I understand this stuff now. I'm going to restate what I think is
true and hope someone will say yea or nay.
The methods retain, release and autorelease all operate on objects, not
pointers.
Copies of pointers are shallow.
apointer = bpointer
will cause apointer to point to the object to which bpointer previously
pointed.
Many of our concerns are about the destiny of the object to which apointer
previously pointed.
A program may call retain many times. A programmer must make sure that for
an object the number of calls to retain is greater than or equal to the
number of calls to retain plus one. The plus one is because when memory is
first allocated for an object, the retain count is one.
When the number of calls to release reaches the number of calls to retain
plus one for an object, the memory allocated to the object is released.
A call to autorelease has the same effect as a call to release except that
the memory persists between the time of the call to autorelease and the
next cycle of the event loop.
The flip side of the above is that an object persists and can be used
between the call to autorelease and the next event loop cycle.
Using autorelease is helpful because in an event loop, there may be a
hierarchy of method invocations. For example, A invokes B which invokes
C... In this case, an object for which C calls autorelease will be
available to B after its invocation to C and of A after its invocation of B.
Equivalent to invoking autorelease in C would be to call release at the end
of A for the object. That is if you assume that A is at the top level of
the methods invoked in the event loop. Invoking autorelease in C avoids
clutter and makes the program easier to write.
Using a factory method avoids the need to call autorelease. The example on
the stepwise site says these are equivalent:
alertString=[NSMutableString stringWithString:@"The following error occured"]
alertString=[[[NSMutableString alloc] initWithString:@"The following error
occured"] autorelease];
Both create an object whose life cycle is from the execution of the
statement to the cleanup done on the autorelease pool at the next cycle of
the event loop.
In the above example, if the pointer, alertString is copied to an instance
variable of an object that persists between event loop cycles, there must
be an invokation of retain before the end of the current event loop cycle.
Usually you would want to invoke release when the containing object is
destroyed - in the dealloc method.
As the cofounder of Next, Ross Perot, would say "It's that simple."
-Danny Swarzman
_______________________________________________
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.