开发者

Class Method Instance Scope?

My question is regarding the scope of the object created in +planet. I have been told that "aut开发者_如何学编程oreleased objects will stay around for the duration of the method/function they were created in" In my example I am assuming that the scope for the planet instance is within main() and not within the method where I do the initial alloc/init?

+(Planet *) planet {
    gPlanetCount++;
    return [[[self alloc] init] autorelease];
}

int main(int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Planet *outerMost;

    outerMost = [Planet planet];
    ...
    ... some code
    ...
    [pool drain];
    return 0;
}

EDIT_001

int main(int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Planet *outerMost;

    outerMost = [[Planet planet] retain]; // Added retain
    ...
    ... some code
    ...
    [outerMost release]; // Added release
    [pool drain];
    return 0;
}

gary


Autoreleased objects are always valid until the surrounding NSAutoreleasePool gets drained. In your example this covers about the main method.

Usually autoreleased objects get released at the end of the run loop. So if you call a method which returns an autoreleased object, it is still valid in your function body, until you either manually drain the surrounding NSAutoreleasePool or you return from a method which was called from the run loop.


Autoreleased objects will stay around until the end of the current run loop (at which time the AutoRelease pool will be drained) or until the AutoRelease pool is drained manually.

It has nothing to do with where they were created - simply what pool is active at the time they are created and when that pool gets drained.

Edit:

The purpose of autorelease pools are hard to understand if you don't put them into context. Using autorelease pools in a procedural application instead of an event-driven application doesn't always make sense. If you had an application sitting in an endless loop then you'll want to drain the pool every now and then. Here's how it might make a bit more sense:

begin application

begin endless loop
    create autorelease pool

    // process messages from the event queue (mouseclicks, keypresses etc) and
    // do stuff here - this might create autoreleased object

    drain autorelease pool
end of endless loop

end application

The autorelease pool is drained on every iteration. It's important to note that the pool might become very full if there is a lot of processing in a single loop iteration. In this case, you can manually drain the pool.


The note about "autoreleased objects will stay around for the duration of the method/function they were created in" is an oversimplification, and it assumes that you're doing things the "cocoa way." Putting "real code" in main() is not the Cocoa way, so you're running into the more literal answer, which others have given you perfectly. Things go away when your auto release pool is flushed.

What the note actually means is that when you're creating objects in a function, you can be sure that the objects can stay around at least until the end of your function, as long as you do not flush the auto release pool yourself. This means that if you're only using the object in that function there is no need to retain and release it within that function. If you are manually flushing the auto release pool you are expected to know the actual rules for when things go away (again, frenetisch applaudierend is correct here) and to mostly ignore the simplification.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜