diff options
author | Gabriel de Dietrich <gabriel.dedietrich@qt.io> | 2016-07-21 18:03:27 -0700 |
---|---|---|
committer | Gabriel de Dietrich <gabriel.dedietrich@qt.io> | 2016-07-22 20:07:58 +0000 |
commit | 8ea51f8aaa426fa14393b83082f9eec076d7d72a (patch) | |
tree | 1eab4329fa9910ae5456e7c6ca3b5388494547f4 /src/plugins/platforms/cocoa/qcocoamenuloader.mm | |
parent | 4bbcc054e03152923938adbd610e954578741755 (diff) |
Make QCocoaMenuLoader a singleton
In some auto-tests, we create several instances of
QGuiApplication (though seldom, if ever, simultaneously).
However, the QCocoaMenuLoader instance was never properly
deallocated, resulting in NSApplication.servicesMenu
to still be assigned. This resulted in an exception being
raised (NSInternalInconsistencyException) the second time
we would construct a QCocoaMenuLoader.
The CPU cycles saving solution is to make QCocoaMenuLoader
a singleton. This approach is also safe since this class'
initialization doesn't depend on any state in QGuiApplication
(even the application name is fetched from either the main
bundle or the app's args).
This also allows us to clean up some code in QCocoaApplication
and QCocoaApplicationDelegate who have suffered from lack of
attention over the years.
Change-Id: Ic4c859d628ab8abd9b469b99c64293582f8e363d
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@theqtcompany.com>
Diffstat (limited to 'src/plugins/platforms/cocoa/qcocoamenuloader.mm')
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoamenuloader.mm | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/plugins/platforms/cocoa/qcocoamenuloader.mm b/src/plugins/platforms/cocoa/qcocoamenuloader.mm index 9b16999d48..22e90f8dc8 100644 --- a/src/plugins/platforms/cocoa/qcocoamenuloader.mm +++ b/src/plugins/platforms/cocoa/qcocoamenuloader.mm @@ -57,6 +57,16 @@ QT_FORWARD_DECLARE_CLASS(QString) @implementation QCocoaMenuLoader ++ (instancetype)sharedMenuLoader +{ + static QCocoaMenuLoader *shared = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + shared = [[self alloc] init]; + }); + return shared; +} + - (instancetype)init { if ((self = [super init])) { @@ -108,9 +118,9 @@ QT_FORWARD_DECLARE_CLASS(QString) // Services item and menu servicesItem = [[NSMenuItem alloc] init]; servicesItem.title = @"Services"; - NSApplication *app = [NSApplication sharedApplication]; - app.servicesMenu = [[[NSMenu alloc] initWithTitle:@"Services"] autorelease]; - servicesItem.submenu = app.servicesMenu; + NSMenu *servicesMenu = [[[NSMenu alloc] initWithTitle:@"Services"] autorelease]; + servicesItem.submenu = servicesMenu; + [NSApplication sharedApplication].servicesMenu = servicesMenu; [appMenu addItem:servicesItem]; [appMenu addItem:[NSMenuItem separatorItem]]; |