prosseek
prosseek

Reputation: 191099

What's the magic of NSAutoreleasePool in Objective-C/cocoa framework?

I found an example of Objective-C/cocoa framework has the following code.

int main()
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Create an array
    NSArray *month = [NSArray arrayWithObjects:@ ... nill];

    [pool drain];
}

For example :

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

Upvotes: 6

Views: 10469

Answers (3)

Kazuki Sakamoto
Kazuki Sakamoto

Reputation: 14009

Q1: The magic is that NSObject -autorelease instance method calls NSAutoreleasePool +addObject: class method. The NSObject instance is pooled in the current NSAutoreleasePool instance. And NSAutoreleasePool -drain instance method calls release method of pooled instances.

It is not the exactly same between Cocoa implementation of Apple and GNUstep, but it is similar.

I'm not sure why month is not released, it should be release by drain.

Q2: You can use NSAutoreleasePool wherever you want to use at. Instantiate a NSAutoreleasePool means the current pool will be changed by the new instance. drain will go back the current pool to the previous instance.

Besides NSApplicationMain never returns. It calls the exit function to exit the application and terminate the process.

Upvotes: 4

2snacc
2snacc

Reputation: 6353

Q1:

You don't need to release the month instance in the example you give because the NSArray class method you're calling (arrayWithObjects:) returns an instance that is autoreleased. By convention in Cocoa, class methods that start with the name of the class will return autoreleased instances of that class. These examples:

[NSString stringWithFormat:@"Holla %@", @"back girl!", nil];
[NSArray arrayWithObjects:@"Holla", @"back", @"girl!", nil];

Will both return instances of their respective objects that are autoreleased.

Upvotes: 3

Graham Christensen
Graham Christensen

Reputation: 849

I'm fairly new to Objective C, but let me give this a crack:

The autorelease pool is a way for Objective C to handle garbage collection in a somewhat easier way than manually.

It does this by counting references, or in this case every time you call "retain" or "release".

So if you have an instance of an object in "a", you could do:

This puts it into the AutoreleasePool: [a autorelease];

This tells the autorelease pool that you want to hold on to it for a while: [a retain];

When you call [pool drain] now, it will notice that you have one reference to a, and it won't deallocate the memory. However, if you later call [a release], the next time [pool drain] is called, it'll see that there are no further references to a remaining, and deallocate the memory.

I have a sneaking suspicion that I just talked myself in circles without making a whole lot of sense, but here is the wikipedia article on reference counting: http://en.wikipedia.org/wiki/Reference_counting

Upvotes: 0

Related Questions