简体   繁体   中英

Menubar App - Is this right?

I'm trying to make an app, which, when the main window isn't visible, shows an icon in the menubar. However, I'm not sure about this code and whether it's ok. I get no errors and no warnings but after a few app switches back and forth between my app and another eg. Twitter or Safari I get

Program received signal: “EXC_BAD_ACCESS”.

Here is my code:

- (void)applicationDidResignActive:(NSNotification*)aNotification
{
        statusItem = [[[NSStatusBar systemStatusBar] 
                       statusItemWithLength:NSVariableStatusItemLength]
                      retain];
        [statusItem setHighlightMode:YES];
        [statusItem setEnabled:YES];

            //Set menubar item's tooltip
        [statusItem setToolTip:@"Nucleus"];
        [statusItem setMenu:theMenu];
            //Set the menubar item's title
        [statusItem setTitle:[NSString stringWithString:@"N"]]; 

}

- (void)applicationDidBecomeActive:(NSNotification*)aNotification
{
    [statusItem release];
}

If the app crashes when you're releasing the statusItem variable there's probability that assigned menu attribute gets over-released. I'm not sure about this since I don't know here theMenu variable is coming from.

I simply replaced [statusItem release]; with [[NSStatusBar systemStatusBar] removeStatusItem:statusItem]; and that worked a treat. I don't want to completely release it as I still need it incase the user switches apps later. Thanks to @Kevin Ballard for that! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM