开发者

Memory management when updating the items in a NSMenu

I'd like to know if this is the correct way to avoid a memory leak in a Cocoa app.

My app has a method that updates an NSMenu's items:

//Remove and Release old Status Scan Menu:
if ([statusMenuScansMenu numberOfItems] !=0开发者_JAVA技巧) {
    for (NSMenuItem *menueItemToBeReleased in [statusMenuScansMenu itemArray]) {
        [statusMenuScansMenu removeItem:menueItemToBeReleased];
        [menueItemToBeReleased release];
    }
}

//New Status Scan Menu:
for (MyObject* myObject in myArray) {
    NSMenuItem * scanMenuItem = [[NSMenuItem alloc] init];
    [scanMenuItem setTitle:[myObject name]];
    [statusMenuScansMenu addItem:scanMenuItem];
}

As you can see, before adding new items I remove all previous items and send release to them. Then I add the new ones.

Is this the best way for memory management?

If I analyze my code in Xcode 4.1, it says that there is a potential memory leak.


It looks like how you are doing it should probably work OK, but it is kind of an odd way to go about it.

If you can require OS X 10.6+, your code could be consolidated to the following:

//Remove old Status Scan Menu:
[statusMenuScansMenu removeAllItems];

//New Status Scan Menu:
for (MyObject* myObject in myArray) {
    NSMenuItem * scanMenuItem = [[[NSMenuItem alloc] 
      initWithTitle:[myObject name] action:NULL keyEquivalent:@""] autorelease];
    [statusMenuScansMenu addItem:scanMenuItem];
}

Note that by adding an autorelease during the creation of the NSMenuItem in the lower loop, there is no need to send the extra release during the menu item removal like in your code. In some sense, an NSMenu acts like an NSArray does with the sub menus and menu items it contains: it retains them. So since you are inserting the newly created NSMenuItem directly into the NSMenu, it's as if the NSMenu is taking ownership of the menu item. As such, you need to counteract the +1 retain count that you get from the alloc/init creation of the item to make sure you don't get a memory leak. In your code, you were counteracting that +1 retain count by sending it an explicit/extra release during the menu item removal, which is kind of roundabout. In the above code that I posted, by adding the autorelease during the creating in the lower loop, the only thing "holding on to" the menu items will the the menu. Then, later, when you call the removeAllItems method, the menu will send a release to each menu item, at which point their retain count should drop to 0, and they'll be deallocated.

If you need to support versions of OS X prior to 10.6, you can use the above code, except substitute [statusMenuScansMenu md_removeAllItems] for [statusMenuScansMenu removeAllItems]. You can then create this md_removeAllItems method in a category on NSMenu like so:

@interface NSMenu (MDAdditions)
- (void)md_removeAllItems;
@end


@implementation NSMenu (MDAdditions)
- (void)md_removeAllItems {
    NSUInteger currentCount = [self numberOfItems];
    for (NSUInteger i = 0; i < currentCount; i++) {
        [self removeItemAtIndex:0];
    }
}
@end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜