Cocoa QPA: Delete singletons on exit

This involves QCocoaApplicationDelegate and QCocoaMenuLoader.

The former has been modernized to use blocks. The latter was
not being deleted previously.

Change-Id: Ic4cbfed2d9598fa04130675b3330d985b9489a21
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
Gabriel de Dietrich 2017-11-20 13:43:38 -08:00
parent a02b371eb2
commit c7faa4ad8f
2 changed files with 20 additions and 33 deletions

View File

@ -86,17 +86,22 @@
QT_USE_NAMESPACE QT_USE_NAMESPACE
QT_BEGIN_NAMESPACE
static QCocoaApplicationDelegate *sharedCocoaApplicationDelegate = nil;
static void cleanupCocoaApplicationDelegate()
{
[sharedCocoaApplicationDelegate release];
}
QT_END_NAMESPACE
@implementation QCocoaApplicationDelegate @implementation QCocoaApplicationDelegate
+ (instancetype)sharedDelegate
{
static QCocoaApplicationDelegate *shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [[self alloc] init];
atexit_b(^{
[shared release];
shared = nil;
});
});
return shared;
}
- (id)init - (id)init
{ {
self = [super init]; self = [super init];
@ -120,7 +125,6 @@ QT_END_NAMESPACE
- (void)dealloc - (void)dealloc
{ {
sharedCocoaApplicationDelegate = nil;
[dockMenu release]; [dockMenu release];
if (reflectionDelegate) { if (reflectionDelegate) {
[[NSApplication sharedApplication] setDelegate:reflectionDelegate]; [[NSApplication sharedApplication] setDelegate:reflectionDelegate];
@ -131,27 +135,6 @@ QT_END_NAMESPACE
[super dealloc]; [super dealloc];
} }
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if (sharedCocoaApplicationDelegate == nil) {
sharedCocoaApplicationDelegate = [super allocWithZone:zone];
qAddPostRoutine(cleanupCocoaApplicationDelegate);
return sharedCocoaApplicationDelegate;
}
}
return nil;
}
+ (QCocoaApplicationDelegate *)sharedDelegate
{
@synchronized(self) {
if (sharedCocoaApplicationDelegate == nil)
[[self alloc] init];
}
return [[sharedCocoaApplicationDelegate retain] autorelease];
}
- (void)setDockMenu:(NSMenu*)newMenu - (void)setDockMenu:(NSMenu*)newMenu
{ {
[newMenu retain]; [newMenu retain];

View File

@ -56,8 +56,12 @@
static QCocoaMenuLoader *shared = nil; static QCocoaMenuLoader *shared = nil;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
shared = [[self alloc] init]; shared = [[self alloc] init];
}); atexit_b(^{
[shared release];
shared = nil;
});
});
return shared; return shared;
} }