可可中默认的 NSAutoreleasePool ?
我询问了 NSAutoreleasePool,并了解这一点在这种情况下,我需要显式分配自动释放池。
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Create an array
NSArray *month = [NSArray arrayWithObjects:@ ... nill];
[pool drain];
}
在我的另一个问题中,我不需要释放NSArray
,因为它将自动释放。
- (NSArray*) getTodayArray
{
...
NSArray *res = [NSArray arrayWithObjects: year, month, nil];
return res;
}
为了使对象自动释放,即使我没有进行任何 NSAutorelease,Cocoa 中也应该分配一些默认的自动释放池。 Xcode 生成的 main 函数非常简单。
int main(int argc, char *argv[])
{
return NSApplicationMain(argc, (const char **) argv);
}
正确吗?如果是的话,何时以及如何分配?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主线程的
NSRunLoop
每次“循环”时都会创建并销毁一个NSAutoreleasePool
。但请注意,当您分离任何后台线程时,您需要为其创建一个自动释放池(因为辅助线程默认情况下没有活动的运行循环)。The main thread's
NSRunLoop
creates and destroys anNSAutoreleasePool
every 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).NSApplicationMain 首先在 NSApplicationMain 中创建 NSAutoreleasePool 实例。正如 @Dave 所说, NSRunLoop 在每个循环中创建并耗尽 NSAutoreleasePool 实例。
如您所知,Apple 的实现和 GNUstep 实现之间并不完全相同,但它们是相似的。
GNUstep libs/gui/ trunk/Source/Functions.m
NSApplicationMain creates NSAutoreleasePool instance at first in NSApplicationMain. And as @Dave said, NSRunLoop creates and drains NSAutoreleasePool instance every loop.
As you know, it is not the exactly same between Apple's implementation and GNUstep implementation but these are similar.
GNUstep libs/gui/trunk/Source/Functions.m