Re: Autopool question
Re: Autopool question
- Subject: Re: Autopool question
- From: "Dennis C. De Mars" <email@hidden>
- Date: Tue, 07 Aug 2001 07:59:04 -0700
on 8/7/01 7:26 AM, Lars Hoss at email@hidden wrote:
>
Hi!
>
>
I have a little question about autorelease pools and how they
>
free objects.
>
Let's assume I have the following method in the class Foo:
>
>
-(MyObject *)object
>
{
>
// create a MyObject instance somehow ...
>
// ref counter on myObject is now 1
>
MyObject *myObject = ...
>
[myObject autorelease];
>
return myObject;
>
}
>
>
Then I have a method in the class Foe:
>
>
-(void)doSomethingWithObject
>
{
>
Foo *fooInstance = [[Foo alloc] init];
>
myObject = [fooInstance object]; // myObject is a class variable
>
[myObject retain]; // ref counter now is 2
>
[fooInstance release];
>
}
>
>
The class Foe needs the instance foo for a longer time.
>
Since I put myObject into the autorelease pool wouldn't
>
it be freed after two event cycles?
No. At the end of the event cycle the autorelease pool is freed and all of
the objects in the pool are released. Your object's retain count is
decreased by one, but since you retained it, it sticks around.
After the pool is released, a fresh pool is created for the next event
cycle. Your object does not remain in the pool for more than one cycle.
- Dennis D.