Here is a trick to help you better understand how to use autorelease pools correctly. Put this in a header somewhere:
static inline void subpool(void (^block)(void)) { NSAutoreleasePool *_pool = [[NSAutoreleasePool alloc] init]; block(); [pool drain]; } … and use it like so: subpool(^{ // do stuff }); And if you need to get stuff out of a subpool: __block NSThingy *t = nil; subpool(^{ t = [other getThingy]; [t retain]; // because getThingy might return autoreleased }); [t doSomething]; [t release]; // balance the earlier retain t = nil; On May 14, 2011, at 6:19 PM, Nick Zitzmann wrote: > > On May 14, 2011, at 5:43 PM, Martin Batholdy wrote: > >> I still have trouble understanding the autorelease pool. >> >> Lets assume an object Z has a method where it gets a string y and returns >> another string x. >> >> Now when an instance of this object is created and the method is invoked, >> x is returned and is used somewhere else. >> >> Now this method of object Z should not be the owner of x right? >> Because x is used elsewhere. >> >> >> So I add x to the autorelease-pool to declare that I am not the owner (and >> won't send it a release message); >> >> x = [[[NSString alloc] init] autorelease]; >> >> >> but where do I release the pool? >> And to which pool is it added? > > Pay attention to the memory management rules: > <http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html> > >> I thought I would create my own pool in the init-method of Z and send it a >> [pool release] message in the alloc-method. >> >> But I haven't seen that so far in sample code ... > > Don't ever do that. You'll wind up popping objects that aren't supposed to be > popped yet, which will lead to a crash. Autorelease pools should never be > used as ivars, globals, or static variables. > > In general, you shouldn't create your own autorelease pools, unless (1) you > are using NSThread or pthread to create new threads, or (2) you have written > a loop that generates a great deal of temporary objects that need to be > popped from time to time to keep memory usage from spiking, and you know what > you're doing. > >> can someone explain me what happens when I add x to the autorelease pool? > > Read the autorelease pools article in the memory management programming guide > (see link above) and it will clear things up. > > Nick Zitzmann > <http://www.chronosnet.com/> > > > > _______________________________________________ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > Please do not post admin requests or moderator comments to the list. > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > Help/Unsubscribe/Update your Subscription: > http://lists.apple.com/mailman/options/cocoa-dev/zarzycki%40apple.com > > This email sent to zarzy...@apple.com _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com