Skip to content

Commit 163b608

Browse files
committed
Avoid bug in mac event loop
1 parent bb44091 commit 163b608

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

modules/yup_events/native/yup_MessageManager_mac.mm

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@
6969
andSelector:@selector(handleQuitEvent:withReplyEvent:)
7070
forEventClass:kCoreEventClass
7171
andEventID:kAEQuitApplication];
72-
73-
[NSApp stop:nil];
7472
});
7573

7674
addMethod(@selector(applicationShouldTerminate:), [](id /*self*/, SEL, NSApplication*)
@@ -371,31 +369,7 @@ void initialiseNSApplication()
371369
}
372370
}
373371

374-
static void runNSApplication()
375-
{
376-
YUP_AUTORELEASEPOOL
377-
{
378-
#if YUP_CATCH_UNHANDLED_EXCEPTIONS
379-
@try
380-
{
381-
[NSApp run];
382-
}
383-
@catch (NSException* e)
384-
{
385-
// An AppKit exception will kill the app, but at least this provides a chance to log it.,
386-
std::runtime_error ex(std::string("NSException: ") + [[e name] UTF8String] + ", Reason:" + [[e reason] UTF8String]);
387-
YUPApplicationBase::sendUnhandledException(&ex, __FILE__, __LINE__);
388-
}
389-
@finally
390-
{
391-
}
392-
#else
393-
[NSApp run];
394-
#endif
395-
}
396-
}
397-
398-
static bool runNSApplicationSlice(int millisecondsToRunFor, Atomic<int>& quitMessagePosted)
372+
static bool runNSApplication(int millisecondsToRunFor, Atomic<int>& quitMessagePosted)
399373
{
400374
jassert(millisecondsToRunFor >= 0);
401375

@@ -409,7 +383,24 @@ static bool runNSApplicationSlice(int millisecondsToRunFor, Atomic<int>& quitMes
409383
if (msRemaining <= 0)
410384
break;
411385

412-
CFRunLoopRunInMode(kCFRunLoopDefaultMode, jmin(1.0, msRemaining * 0.001), true);
386+
#if YUP_CATCH_UNHANDLED_EXCEPTIONS
387+
@try
388+
{
389+
#endif
390+
CFRunLoopRunInMode(kCFRunLoopDefaultMode, jmin(1.0, msRemaining * 0.001), true);
391+
392+
#if YUP_CATCH_UNHANDLED_EXCEPTIONS
393+
}
394+
@catch (NSException* e)
395+
{
396+
// An AppKit exception will kill the app, but at least this provides a chance to log it.,
397+
std::runtime_error ex(std::string("NSException: ") + [[e name] UTF8String] + ", Reason:" + [[e reason] UTF8String]);
398+
YUPApplicationBase::sendUnhandledException(&ex, __FILE__, __LINE__);
399+
}
400+
@finally
401+
{
402+
}
403+
#endif
413404
}
414405
}
415406

@@ -431,11 +422,9 @@ static void shutdownNSApp()
431422

432423
constexpr int millisecondsToRunFor = static_cast<int>(1000.0f / 60.0f); // TODO
433424

434-
runNSApplication();
435-
436425
while (quitMessagePosted.get() == 0)
437426
{
438-
if (runNSApplicationSlice(millisecondsToRunFor, quitMessagePosted))
427+
if (runNSApplication(millisecondsToRunFor, quitMessagePosted))
439428
{
440429
if (loopCallback)
441430
loopCallback();
@@ -475,7 +464,7 @@ static void shutdownNSApp()
475464
jassert(millisecondsToRunFor >= 0);
476465
jassert(isThisTheMessageThread()); // must only be called by the message thread
477466

478-
return runNSApplicationSlice(millisecondsToRunFor, quitMessagePosted);
467+
return runNSApplication(millisecondsToRunFor, quitMessagePosted);
479468
}
480469
#endif
481470

0 commit comments

Comments
 (0)