      // Add a new, empty (null), NSModalSession to the stack.
      // It will become active the next time QEventDispatcher::processEvents is called.
      // A QCocoaModalSessionInfo is considered pending to become active if the widget pointer 
-@@ -958,16 +999,20 @@
+@@ -958,16 +1008,20 @@
      // when we stop the _current_ modal session (which is the session on top of
      // the stack, and might not belong to 'widget'). 
      int stackSize = cocoaModalSessionStack.size();

 #endif
 
     if (d->interrupt) {
@@ -1055,8 +1101,9 @@
             // [NSApplication run] is running the event loop, we
             // delayed stopping it until now (to let cocoa process 
             // pending cocoa events first).

                         // INVARIANT: Someone called [NSApp stopModal:] from outside the event
                         // dispatcher (e.g to stop a native dialog). But that call wrongly stopped
                         // 'session' as well. As a result, we need to restart all internal sessions:
                         d->temporarilyStopAllModalSessions();
                     }
+                    // Clean up the modal session list, call endModalSession.
+                    if (d->cleanupModalSessionsNeeded) {
+                        d->cleanupModalSessions();
+                    }
                     retVal = true;
                 } else do {
                     // Dispatch all non-user events (but que non-user events up for later). In
@@ -823,6 +867,7 @@
         if (info.session) {
             [NSApp endModalSession:info.session];
             info.session = 0;
