0% found this document useful (0 votes)
28 views

Helpful For Iphone

The NSAutoreleasePool manages memory for objects that are autoreleased. In the code example, the NSArray instance returned by arrayWithObjects: is autoreleased, so it will be released when the pool is drained. When using Xcode's default main, an autorelease pool can be added before NSApplicationMain to manage autoreleased objects created beforehand.

Uploaded by

Hasan Ibna Akbar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Helpful For Iphone

The NSAutoreleasePool manages memory for objects that are autoreleased. In the code example, the NSArray instance returned by arrayWithObjects: is autoreleased, so it will be released when the pool is drained. When using Xcode's default main, an autorelease pool can be added before NSApplicationMain to manage autoreleased objects created beforehand.

Uploaded by

Hasan Ibna Akbar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 1

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------1

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Question:
------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];
}

Q1 : What's the magic behind this (Why do I need to have the


NSAutoreleasePool code?)? What magic is happening between the
NSAutoreleasePool and pool drain block? I see I don't need to
release*month myself. Is this because it's inside the NSAutoreleasePool
and pool drain block?

Q2 : With Xcode, I'm already given the main() function. In this


case, how can I use the NSAutoreleasePool and pool drain?
For example :
int main(int argc, char *argv[])
{
//NSAutoreleasePool *pool = [[[NSAutoreleasePool] alloc]
init];
return NSApplicationMain(argc, (const char **) argv);
}
Answer:
-------

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.

You might also like