每个对象一个 AutoreleasePool?
我不能在 main()
中有一个“大”的 NSAutoreleasePool
– 我不被允许碰它。那么每个对象有一个池怎么样呢?
struct MacGuiEngine
{
// members …
ScopedAutoreleasePool pool;
};
struct MacFontEngine
{
// members …
ScopedAutoreleasePool pool;
};
这是一个有效的“模式”吗?
I can't have a "big" NSAutoreleasePool
in main()
– I'm not allowed to touch it. So what's about having one pool per object?
struct MacGuiEngine
{
// members …
ScopedAutoreleasePool pool;
};
struct MacFontEngine
{
// members …
ScopedAutoreleasePool pool;
};
Is this a valid "pattern"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你说你不能触摸
main()
时,你什么时候调用UIApplicationMain()
?我假设这是 iOS,因为您不需要在 Mac 上的main()
中创建自动释放池。无论您在哪里调用UIApplicationMain()
,您都需要顶级自动释放池。请记住,系统会为每个事件循环自动创建一个自动释放池,因此您通常不需要创建一个。我最初的实验是,从 iPad 上的
main()
中删除它至少不会导致任何泄漏。您可以通过在__NSAutoreleaseNoPool()
上设置断点来找出答案。唯一关心的是在事件循环之前调用的方法。我相信,如果应用程序委托是以编程方式生成的,那么它的-init
可能会在事件循环之前被调用。但即使在应用程序委托的 +initialize 中创建自动释放对象也没有给我造成任何泄漏。如果有任何地方你需要一个自动释放池(其中
__NSAutoreleaseNoPool()
被调用,并且你看到类似“没有池的对象自动释放——只是泄漏”),那么你只需要在那个方法:但我仍然有点不知道如何启动主运行循环,但无法创建自动释放池。
When you say you can't touch
main()
, when do you callUIApplicationMain()
? I'm assuming this is iOS, since you don't need to create an autorelease pool inmain()
on Mac. Wherever you callUIApplicationMain()
is where you want the top-level autorelease pool.Remember, an autorelease pool is automatically created for you for each event loop, so you generally don't need to create one. My initial experiments are that removing it from
main()
on iPad at least didn't cause any leaks. You can find out by setting a breakpoint on__NSAutoreleaseNoPool()
. The only concern would be methods called prior to the event loop. I believe if the application delegate is generated programmatically, then its-init
may be called prior to an event loop. But even making autoreleased objects in app delegate's+initialize
didn't cause any leaks for me.If there are any places you need an autorelease pool (where
__NSAutoreleaseNoPool()
gets called and you see something like "object autoreleased without pool -- just leaking"), then you just need to create a pool in that method:But I'm still a little at a loss of how you're starting your main runloop, but can't create an autorelease pool.