macosx: fix exit segfault

Refs #70803

Change-Id: I0471c01493e22ba97b8ad08df4caca7a3aaba223
diff --git a/src/AppDelegate.mm b/src/AppDelegate.mm
index 355ba5c..197b320 100644
--- a/src/AppDelegate.mm
+++ b/src/AppDelegate.mm
@@ -30,7 +30,7 @@
 #import "AppDelegate.h"
 
 #import <callmodel.h>
-
+#import <qapplication.h>
 #import <accountmodel.h>
 #import <protocolmodel.h>
 #import <QItemSelectionModel>
@@ -54,6 +54,9 @@
 
     [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
 
+    NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
+    [appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
+
     if([self checkForRingAccount]) {
         [self showMainWindow];
     } else {
@@ -133,4 +136,16 @@
     return YES;
 }
 
+- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
+{
+    delete CallModel::instance()->QObject::parent();
+    [[NSApplication sharedApplication] terminate:self];
+}
+
+-(void)applicationWillTerminate:(NSNotification *)notification
+{
+    delete CallModel::instance()->QObject::parent();
+    [[NSApplication sharedApplication] terminate:self];
+}
+
 @end