Re: why does this leak?
Re: why does this leak?
- Subject: Re: why does this leak?
- From: Robert Dell <email@hidden>
- Date: Mon, 13 Feb 2006 21:08:00 -0500
Charlton Wilbur wrote:
On Feb 13, 2006, at 6:41 PM, Robert Dell wrote:
I update my menus by first removing the items. I used the following
code:
while ([mySingMenu numberOfItems] > 0)
{
[mySingMenu removeItemAtIndex: 0];
};
From what i was told on this list, removing items from the list
automatically releases them. why is it i get 2 leaks per remove
without a predeclared release as such?
How do you know you're getting "2 leaks per remove"? If you're paying
attention to [object retainCount], DON'T. All sorts of other objects
in the Cocoa framework may send retain messages to your objects; all
you need to worry about is whether *you* have sent a balanced number of
alloc/copy/retain and release/autorelease messages. In the case of
collections which retain their member objects, you still have to pair
retains and releases; it's just that adding the object to the
collection *also* retains it, and removing it *also* releases it.
Further, I see nothing in the documentation that indicates that -
[NSMenu addItem:] sends a retain message, or -[NSMenu
removeItemAtIndex:] sends a release message. If you compare this to
the documentation for -[NSMutableSet addObject:] and -[NSMutableSet
removeObjectAtIndex:],for instance, you'll see that both of the latter
are explicitly documented to send retain and release messages,
respectively. I suspect you're confusing the behavior of NSMenu with
the behavior of NSMutable(Set|Array|Dictionary).
Charlton
how do i know i was getting 2 leaks per remove? well, the clincher was when leaks was done on the running app and i had a few thousand leaks with the label "NSMenuItem" attached to it along with an associated string of the same size (the menu updater gets updated every 1/10 of a second, i know, a little too often. it's going to be slowed down). When i added the release, the leaks command stopped reporting THOSE leaks (now only a few thousand more leaks to track down).
by the way, have you ran leaks against the finder? it's depressing...
I think it was because I was forced to add a retain to it in order to keep the program from crashing.
please see the code below:
NSLog(@"initMenus started",nil);
NSMutableString *pathToSongsFolder = [[[NSMutableString alloc] init] autorelease];
NSFileManager *fileManager = [NSFileManager defaultManager];
id tempmenuitem;
int counter = 0;
[pathToSongsFolder setString: [@"~/Documents/Simutronics/Songs/" stringByExpandingTildeInPath]];
[pathToSongsFolder appendString: @"/"];
NSArray *foldercontents = [fileManager directoryContentsAtPath: pathToSongsFolder];
while ([mySingMenu numberOfItems] > 0)
{
[[mySingMenu itemAtIndex: 0] release];
[mySingMenu removeItemAtIndex: 0];
};
for (counter = 0; counter < [foldercontents count]; counter++)
{
[pathToSongsFolder setString: [foldercontents objectAtIndex: counter]];
if ([pathToSongsFolder rangeOfString: @".song" options: NSCaseInsensitiveSearch].length)
{
tempmenuitem = [[mySingMenu insertItemWithTitle: [NSString stringWithString: pathToSongsFolder] action: @selector(singSong:) keyEquivalent: @"" atIndex: [mySingMenu numberOfItems]] retain];
[tempmenuitem setTarget: self];
[tempmenuitem setState: NSOffState];
[tempmenuitem setEnabled: YES];
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden