I asked about NSAutoreleasePool, and understand that I need explicitly allocate autorelease pool in this case.
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Create an array
NSArray *month = [NSArray arrayWithObjects:@ ... nill];
[pool drain];
}
In my other question, I don’t need release NSArray as it will be autoreleased.
- (NSArray*) getTodayArray
{
...
NSArray *res = [NSArray arrayWithObjects: year, month, nil];
return res;
}
In order to be the object autoreleased even though I didn’t make any NSAutorelease, there should be some default autorelease pool allocated in Cocoa. The Xcode generated main function is pretty simple.
int main(int argc, char *argv[])
{
return NSApplicationMain(argc, (const char **) argv);
}
Is it correct? If so, when and how it’s allocated?
The main thread’s
NSRunLoopcreates and destroys anNSAutoreleasePoolevery time it “loops”. Note, however, that when you split off any background thread, you need to create an autorelease pool for it (since secondary threads, by default, do not have active run loops).