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: George Nachman <email@hidden>
- Date: Thu, 28 Oct 2010 09:25:59 -0700
Thanks for the response, Sherm. I added the code as you suggested but
the leak persists. I originally discovered this issue because my
program would grow slowly over several days; the code I originally
posted was being called once a second from the event loop, which I
guess would allow the autorelease pools to be freed.
In trying to figure this out, I downloaded the binary distribution of
my program that someone else built (before I took it over). That
version does not leak. But if I check out the code they used and build
my own deployment build, that does leak.
What could be different about my build environment that would cause this?
On Wed, Oct 27, 2010 at 9:00 PM, Sherm Pendley <email@hidden> wrote:
> 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