Recent

Author Topic: [NSAlert runModal] may not be invoked inside of transaction begin/commit pair  (Read 3372 times)

olly

  • New Member
  • *
  • Posts: 42
Hello, my application sometimes crashes when showing a form/dialog. It happens with native dialogs (OpenDialog & SaveDialog) and with normal forms.

Here is a trace of when it crashes by calling ShowModal on a TForm (SIMBA.SIMPLEFILEDIALOG in this case)

Lazarus 2.0.8 & FPC 3.0.4

Code: [Select]
2020-07-05 23:30:34.355 Simba[3532:95001] *** Terminating app due to uncaught exception 'NSGenericException', reason: '-[NSAlert runModal] may not be invoked inside of transaction begin/commit pair, or inside of transaction commit (usually this means it was invoked inside of a view's -drawRect: method.)'
*** First throw call stack:
(
0   CoreFoundation                      0x00007fff2e152be7 __exceptionPreprocess + 250
1   libobjc.A.dylib                     0x00007fff66f2a5bf objc_exception_throw + 48
2   CoreFoundation                      0x00007fff2e152a45 +[NSException raise:format:] + 189
3   AppKit                              0x00007fff2bbdfad7 _NSRunModal + 152
4   AppKit                              0x00007fff2b6e55bf -[NSAlert runModal] + 225
5   Simba                               0x000000010ab7e6fb COCOAINT_$$_COCOAPROMPTUSER$crcF62BAB40 + 1187
6   Simba                               0x000000010ab7e8fb COCOAINT$_$TCOCOAWIDGETSET_$__$$_PROMPTUSER$crc484A2DB2 + 83
7   Simba                               0x000000010ab2ecf7 LCLINTF_$$_PROMPTUSER$ANSISTRING$LONGINT$PLONGINT$LONGINT$LONGINT$LONGINT$$LONGINT + 95
8   Simba                               0x000000010ac5fa6b DIALOGS_$$_MESSAGEDLG$ANSISTRING$TMSGDLGTYPE$TMSGDLGBUTTONS$LONGINT$$TMODALRESULT + 187
9   Simba                               0x000000010aaa90e3 SIMBA.MAIN$_$TSIMBAFORM_$__$$_CUSTOMEXCEPTIONHANDLER$TOBJECT$EXCEPTION + 1283

// The application is done for. Above is just exception handling which tries to launch a dialog and dies.

10  Simba                               0x000000010aa93cec FORMS$_$TAPPLICATION_$__$$_HANDLEEXCEPTION$TOBJECT + 436
11  Simba                               0x000000010aba8207 LCLMESSAGEGLUE_$$_DELIVERMESSAGE$TOBJECT$formal$$INT64 + 343
12  Simba                               0x000000010aba8541 LCLMESSAGEGLUE_$$_LCLSENDPAINTMSG$TCONTROL$HDC$PPAINTSTRUCT$$INT64 + 81
13  Simba                               0x000000010aba6ae4 COCOAWSCOMMON$_$TLCLCOMMONCALLBACK_$__$$_DRAW$NSGRAPHICSCONTEXT$CGRECT$CGRECT + 484
14  Simba                               0x000000010ab8042c -[TCocoaCustomControl drawRect:] + 380
15  AppKit                              0x00007fff2b4d6d8f _NSViewDrawRect + 139
16  AppKit                              0x00007fff2b4d609e -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inContext:stopAtLayerBackedViews:] + 2170
17  AppKit                              0x00007fff2b4d549a -[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] + 1271
18  QuartzCore                          0x00007fff39bbec5d CABackingStoreUpdate_ + 595
19  QuartzCore                          0x00007fff39c1e7d9 ___ZN2CA5Layer8display_Ev_block_invoke + 53
20  QuartzCore                          0x00007fff39bbe162 -[CALayer _display] + 2103
21  AppKit                              0x00007fff2b4d4e8a -[_NSBackingLayer display] + 537
22  AppKit                              0x00007fff2b436977 -[_NSViewBackingLayer display] + 800
23  QuartzCore                          0x00007fff39bbd1fd _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 757
24  QuartzCore                          0x00007fff39b9b4b6 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 334
25  QuartzCore                          0x00007fff39b9a0a0 _ZN2CA11Transaction6commitEv + 644
26  AppKit                              0x00007fff2b4eb591 __62+[CATransaction(NSCATransaction) NS_setFlushesWithDisplayLink]_block_invoke + 266
27  AppKit                              0x00007fff2bc0a850 ___NSRunLoopObserverCreateWithHandler_block_invoke + 41
28  CoreFoundation                      0x00007fff2e0d63c5 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
29  CoreFoundation                      0x00007fff2e0d62f7 __CFRunLoopDoObservers + 457
30  CoreFoundation                      0x00007fff2e0d5895 __CFRunLoopRun + 874
31  CoreFoundation                      0x00007fff2e0d4ece CFRunLoopRunSpecific + 462
32  HIToolbox                           0x00007fff2cd03abd RunCurrentEventLoopInMode + 292
33  HIToolbox                           0x00007fff2cd036f4 ReceiveNextEventCommon + 359
34  HIToolbox                           0x00007fff2cd03579 _BlockUntilNextEventMatchingListInModeWithFilter + 64
35  AppKit                              0x00007fff2b34b829 _DPSNextEvent + 883
36  AppKit                              0x00007fff2b34a070 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1352
37  Simba                               0x000000010ab7417c -[TCocoaApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 92
38  Simba                               0x000000010ab745ec COCOAINT$_$TCOCOAWIDGETSET_$__$$_APPRUNMESSAGES$BOOLEAN$NSDATE + 116
39  Simba                               0x000000010ab7467d COCOAINT$_$TCOCOAWIDGETSET_$__$$_APPPROCESSMESSAGES + 37
40  Simba                               0x000000010aa8d8b0 FORMS$_$TCUSTOMFORM_$__$$_SHOWMODAL$$LONGINT + 864
41  Simba                               0x000000010adfb54b SIMBA.SIMPLEFILEDIALOG$_$TSIMBAFILEDIALOG_$__$$_EXECUTE$$BOOLEAN + 83
42  Simba                               0x000000010aab4ec9 SIMBA.SCRIPTTABSFORM$_$TSIMBASCRIPTTABSFORM_$__$$_OPEN + 401
43  Simba                               0x000000010aaab4cb SIMBA.MAIN$_$TSIMBAFORM_$__$$_MENUOPENCLICK$TOBJECT + 35
44  Simba                               0x000000010abc5d80 CONTROLS$_$TCONTROL_$__$$_CLICK + 104
45  Simba                               0x000000010ac06181 COMCTRLS$_$TTOOLBUTTON_$__$$_CLICK + 25
46  Simba                               0x000000010ac04084 COMCTRLS$_$TTOOLBUTTON_$__$$_MOUSEUP$TMOUSEBUTTON$TSHIFTSTATE$LONGINT$LONGINT + 508
47  Simba                               0x000000010abc4e6a CONTROLS$_$TCONTROL_$__$$_DOMOUSEUP$TLMMOUSE$TMOUSEBUTTON + 234
48  Simba                               0x000000010abc5aa8 CONTROLS$_$TCONTROL_$__$$_WMLBUTTONUP$TLMMOUSE + 232
49  Simba                               0x000000010aa61523 SYSTEM$_$TOBJECT_$__$$_DISPATCH$formal + 203
50  Simba                               0x000000010abc37d6 CONTROLS$_$TCONTROL_$__$$_PERFORM$LONGWORD$WPARAM$LPARAM$$LRESULT + 86
51  Simba                               0x000000010abb77c1 CONTROLS$_$TWINCONTROL_$__$$_ISCONTROLMOUSEMSG$formal$$BOOLEAN + 465
52  Simba                               0x000000010abb8c09 CONTROLS$_$TWINCONTROL_$__$$_WNDPROC$TLMESSAGE + 369
53  Simba                               0x000000010aba8190 LCLMESSAGEGLUE_$$_DELIVERMESSAGE$TOBJECT$formal$$INT64 + 224
54  Simba                               0x000000010aba688d COCOAWSCOMMON$_$TLCLCOMMONCALLBACK_$__$$_DELIVERMESSAGE$formal$$LRESULT + 45
55  Simba                               0x000000010aba5b01 COCOAWSCOMMON$_$TLCLCOMMONCALLBACK_$__$$_MOUSEUPDOWNEVENT$NSEVENT$BOOLEAN$BOOLEAN$$BOOLEAN + 1097
56  Simba                               0x000000010ab80a89 -[TCocoaCustomControl mouseUp:] + 97
57  AppKit                              0x00007fff2b4f168f -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2738
58  AppKit                              0x00007fff2b4f09b9 -[NSWindow(NSEventRouting) sendEvent:] + 349
59  Simba                               0x000000010ab98028 -[TCocoaWindow sendEvent:] + 448
60  AppKit                              0x00007fff2b4eed44 -[NSApplication(NSEvent) sendEvent:] + 352
61  Simba                               0x000000010ab74008 -[TCocoaApplication sendEvent:] + 880
62  Simba                               0x000000010ab7460f COCOAINT$_$TCOCOAWIDGETSET_$__$$_APPRUNMESSAGES$BOOLEAN$NSDATE + 151
63  Simba                               0x000000010ab7467d COCOAINT$_$TCOCOAWIDGETSET_$__$$_APPPROCESSMESSAGES + 37
64  Simba                               0x000000010aa93e45 FORMS$_$TAPPLICATION_$__$$_HANDLEMESSAGE + 45
65  Simba                               0x000000010aa9433c FORMS$_$TAPPLICATION_$__$$_RUNLOOP + 76
66  Simba                               0x000000010ab738cd -[TCocoaApplication run] + 53
67  Simba                               0x000000010ab74573 COCOAINT$_$TCOCOAWIDGETSET_$__$$_APPRUN$TAPPLICATIONMAINLOOP + 83
68  Simba                               0x000000010aa942e8 FORMS$_$TAPPLICATION_$__$$_RUN + 96
69  Simba                               0x000000010aa562bc PASCALMAIN + 1196
70  Simba                               0x000000010aa69d79 FPC_SYSTEMMAIN + 41
)
libc++abi.dylib: terminating with uncaught exception of type NSException
zsh: abort      sudo ./Simba

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
that's a known issue with unknown solution

However, in your case, it seems like you're getting an exception while processing PAINT event.
so the app crashed while crashing...
« Last Edit: July 06, 2020, 05:42:26 pm by skalogryz »

winni

  • Hero Member
  • *****
  • Posts: 3197
Hi!

Solution 1: Disable the debugger - no crashes anymore

Solution 2: Use fpc 3.2

Winni


Hansaplast

  • Hero Member
  • *****
  • Posts: 674
  • Tweaking4All.com
    • Tweaking4All
Maybe it is coincidence, since Skalogryz is most certainly way more knowledgeable about this, but I found it to happen under macOS 10.13 (could have been 10.14 as well, but not under 10.15) when doing something in the TForm OnPaint event, which could potentially trigger some sorts of repaint.

Not sure if this is helpful, just thought I'd mention it.

Clover

  • New Member
  • *
  • Posts: 46
The [runModal] issue started happening in my app and I traced it to an exception occurring in my OnPaint event, while a MessageDlg was currently showing. (The OnPaint was trying to read from a file which was already open when the MessageDlg got shown.)

In other words the OnPaint exception caused a modal while another modal was showing.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
In other words the OnPaint exception caused a modal while another modal was showing.
not necessary "while modal was showing".
OnPaint exception is causing a default Exception handler to kick in.
LCL default Exception handler shows a modal dialog suggesting to continue or to terminate the application.

But modal dialogs should not be shown during Paint event at all (in Cocoa world).

It's either CocoaWS needs to suppress any exceptions occurred during Paint
OR there might be a way for Cocoa to "stop" painting in order to show the dialog
OR "showModal" method in CocoaWS needs to detect a paint event and silently do-nothing, if called during paint

ChrisR

  • Full Member
  • ***
  • Posts: 247
I also get errors like
  *** Terminating app due to uncaught exception 'NSGenericException', reason: '-[NSAlert runModal] may not be invoked inside of transaction begin/commit pair, or inside of transaction commit (usually this means it was invoked inside of a view's -drawRect: method.)'
*** First throw call stack:

in my OpenGL applications when I want to display a dialog describing why a function failed. These work fine on Linux and Windows, but create an exception for MacOS. Does Lazarus have a simple amodal window or a deferred modal dialog that could be used? In my case, I am presenting a graphical dialog when I want to explain to a user why a function failed, and it is this error dialog that causes the app to crash on MacOS.

Clover

  • New Member
  • *
  • Posts: 46
It's either CocoaWS needs to suppress any exceptions occurred during Paint
OR there might be a way for Cocoa to "stop" painting in order to show the dialog
OR "showModal" method in CocoaWS needs to detect a paint event and silently do-nothing, if called during paint

Ah, so it's simply caused by trying to show a modal in an OnPaint, thanks for that.

So, I could perhaps put all my OnPaint code inside a try..except and silently queue an event to show the error?

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
So, I could perhaps put all my OnPaint code inside a try..except and silently queue an event to show the error?
That would be the most desired approach.

ChrisR

  • Full Member
  • ***
  • Posts: 247
My solution to this problem is to display a user notification. The seems like the native MacOS way to tell users something without interfering with the application. I am including a simple project that shows this. In my own code I have a wrapper that emulates this for Linux and Windows.


 

TinyPortal © 2005-2018