Re: Adding and removing menu items causes memory usage to grow
Re: Adding and removing menu items causes memory usage to grow
- Subject: Re: Adding and removing menu items causes memory usage to grow
- From: Sherm Pendley <email@hidden>
- Date: Thu, 28 Oct 2010 00:00:55 -0400
On Wed, Oct 27, 2010 at 12:45 PM, George Nachman <email@hidden> 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 (email@hidden)
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:
This email sent to email@hidden