NSStatusBar menu bar icon NSMenu leaking CFData

◇◆丶佛笑我妖孽 提交于 2019-12-13 05:24:32

问题


I have a weird data leak and hope someone can explain what is going wrong here. The minimal working example project is as follows:

  1. Create a new Objective-C macOS project (Cocoa-App).
  2. Delete everything except Info.plist and main.m
  3. In Info.plist do:
    • Delete the entry Main nib file base name.
    • Change Principal class to "AppHook".
    • Add LSUIElement and set it to YES.

Next, change main.m to:

#import <Cocoa/Cocoa.h>
#import "AppHook.h"

int main(int argc, const char * argv[]) {
    [[AppHook sharedApplication] run];
    return 0;
}

And add these two new files: AppHook.h

#import <Cocoa/Coco a.h>

@interface AppHook : NSApplication <NSApplicationDelegate, NSMenuDelegate>
@property (strong) NSStatusItem *barItem;
@end

and AppHook.m:

#import "AppHook.h"

@implementation AppHook
- (instancetype)init {
    self = [super init];
    self.delegate = self;
    return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    self.barItem = [NSStatusBar.systemStatusBar statusItemWithLength:NSVariableStatusItemLength];
    self.barItem.highlightMode = YES;
    self.barItem.title = @"yep";
    self.barItem.menu = [[NSMenu alloc] initWithTitle:@"M"];
    self.barItem.menu.delegate = self;
    self.barItem.menu.autoenablesItems = NO;
}
- (NSInteger)numberOfItemsInMenu:(NSMenu*)menu {
    return 1;
}
- (BOOL)menu:(NSMenu*)menu updateItem:(NSMenuItem*)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel {
    item.title = [NSString stringWithFormat:@"%@", [NSDate date]];
    return YES;
}
@end

When running this application with Instruments and the Leaks template, I get following CFData leak:

0   Malloc      +1  1   00:03.319.300   AppKit  _DPSNextEvent
1   CFRetain    +1  2   00:03.319.303   AppKit  CopyCarbonUIElementAttributeValue
2   CFRelease   -1  1   00:03.319.310   AppKit  _DPSNextEvent

This happens every time the status menu is opened and closed. So hitting the menu icon repeatedly will create a bunch of leaks.

Nothing fancy here, just a subclass of NSApplication (I need to override sendEvent(_:)).


回答1:


So ... after quite a long run it happend again.

Short answer:

restart your Mac.

Long answer:

As you can see my latest comment is nearly a month ago. Since then I restarted my mac twice and always checked Xcode Profiler before. For those two reboots the Profiler didn't show any leaks. I am not sure when the last reboot took place, roughly two weeks ago. Today I noticed that Profiler is showing the same leaks again.

What did not work:

  • Cleaning the caches and DerivedData folder.
  • Quit Xcode and restart it.
  • Doing both and any other possible combination of these.

Seems to be a bug with macOS itself. I would create a bugreport but honestly, I wouldn't even know what to write. Reproducability is just by chance :-/
Also, it does not depend on a particular project. Used this sample project as reference and kept it without any changes in the last month.



来源:https://stackoverflow.com/questions/53182641/nsstatusbar-menu-bar-icon-nsmenu-leaking-cfdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!