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”?
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-initmay be called prior to an event loop. But even making autoreleased objects in app delegate’s+initializedidn’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.