On Wed, Oct 27, 2010 at 12:45 PM, George Nachman <gnach...@llamas.org> wrote: > > I was tracking down memory growth in my application and isolated it to > the repeated adding and removing of menu items. Doing this causes a > growth of about 1MB: > > for (int i = 0; i < 1000; ++i) {
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init]; > NSMenu *aMenu = [[NSMenu alloc] init]; > NSMenuItem* i = [[NSMenuItem alloc] initWithTitle: @"AAA" > action:nil keyEquivalent:@""]; > [aMenu addItem:i]; > [i release]; > [aMenu removeItemAtIndex:0]; > [aMenu release]; [myPool drain]; > } > > The Leaks instrument doesn't show any leaks. Allocations shows a whole > bunch of different objects growing, but CFBasicHash is the worst > offender, with lots of allocations in -[NSMenu insertItem:atIndex:] > and -[NSMenu removeItemAtIndex:]. At a guess, I'd say that -[NSMenu removeItemAtIndex:] autoreleases the removed item, and since you're in a loop here that doesn't pump the run loop, the autorelease pool isn't being drained. Try allocating an AR pool at the top of your loop, and sending it a -drain at the bottom, like I've added above. sherm-- -- Cocoa programming in Perl: http://camelbones.sourceforge.net _______________________________________________ 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