title: NSAlert runModal may not be invoked inside of transaction commit (usually this means it was invoked inside of a view's -drawRect: method)
body: <p>I have a Xamarin.Mac app that crashes when trying to display an alert from the GetView method of a NSOutlineViewDelegate. </p>
  
  <p>The code is the following:</p>
  
  <pre><code>internal class ItemsDelegate : NSOutlineViewDelegate
  {
      public override NSView GetView(NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item)
      {
          try
          {
              //code to return the NSView
          }
          catch (Exception ex)
          {
  
              NSAlert alert = new NSAlert();
              alert.InformativeText = message;
  
              alert.RunModal();
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>So, what it happens is that a controlled exception is thrown in the code to return the NSView.
  When trying to show it with the NSAlert, in the catch block and the "alert.RunModal()" is executed, the app crashes.</p>
  
  <p>The crash exception is the following:</p>
  
  <pre><code>Terminating app due to uncaught exception 'NSGenericException', reason: '-[NSAlert runModal] may not be invoked inside of transaction commit (usually this means it was invoked inside of a view's -drawRect: method.)'
  
  First throw call stack:
  (
      0   CoreFoundation                      0x00007fffca9976fb _exceptionPreprocess + 171
      1   libobjc.A.dylib                     0x00007fffdf33ba2a objc_exception_throw + 48
      2   CoreFoundation                      0x00007fffcaa149a5 +[NSException raise:format:] + 197
      3   AppKit                              0x00007fffc8d2eeaa _NSRunModal + 266
      4   AppKit                              0x00007fffc876c591 -[NSAlert runModal] + 270
      5   macplastic                          0x0000000107c97b39 xamarin_dyn_objc_msgSend + 217
      6   ???                                 0x0000000116c14f2b 0x0 + 4676734763
      7   ???                                 0x0000000116b06825 0x0 + 4675627045
      8   ???                                 0x0000000116a62808 0x0 + 4674955272
      9   ???                                 0x0000000116a62b8e 0x0 + 4674956174
      10  macplastic                          0x0000000107d9a340 mono_jit_runtime_invoke + 2272
      11  macplastic                          0x0000000107e4b3c8 do_runtime_invoke + 88
      12  macplastic                          0x0000000107e4b2d6 mono_runtime_invoke + 102
      13  macplastic                          0x0000000107c9583d xamarin_invoke_trampoline + 5725
      14  macplastic                          0x0000000107c965bd xamarin_arch_trampoline + 189
      15  macplastic                          0x0000000107c979b1 xamarin_x86_64_common_trampoline + 110
      16  AppKit                              0x00007fffc85b4f84 -[NSTableView(NSTableViewViewBased) makeViewForTableColumn:row:] + 76
      17  AppKit                              0x00007fffc85b495b -[NSTableRowData _addViewToRowView:atColumn:row:] + 308
      18  AppKit                              0x00007fffc85b469f -[NSTableRowData _addViewsToRowView:atRow:] + 204
      19  AppKit                              0x00007fffc85b2e47 -[NSTableRowData _initializeRowView:atRow:] + 390
      20  AppKit                              0x00007fffc85b144d -[NSTableRowData _addRowViewForVisibleRow:withPriorView:] + 398
      21  AppKit                              0x00007fffc85b1208 -[NSTableRowData _addRowViewForVisibleRow:withPriorRowIndex:inDictionary:withRowAnimation:] + 316
      22  AppKit                              0x00007fffc85b0030 -[NSTableRowData _unsafeUpdateVisibleRowEntries] + 1647
      23  AppKit                              0x00007fffc85af923 -[NSTableRowData updateVisibleRowViews] + 232
      24  AppKit                              0x00007fffc85af27a -[NSTableView layout] + 178
      25  AppKit                              0x00007fffc8d19afc _NSViewLayout + 450
      26  AppKit                              0x00007fffc8550074 -[NSView _doLayout] + 126
      27  AppKit                              0x00007fffc854fbf9 -[NSView _layoutSubtreeWithOldSize:] + 497
      28  AppKit                              0x00007fffc854fec7 -[NSView _layoutSubtreeWithOldSize:] + 1215
      29  AppKit                              0x00007fffc854fec7 -[NSView _layoutSubtreeWithOldSize:] + 1215
      30  AppKit                              0x00007fffc854fec7 -[NSView _layoutSubtreeWithOldSize:] + 1215
      31  AppKit                              0x00007fffc854fec7 -[NSView _layoutSubtreeWithOldSize:] + 1215
      32  AppKit                              0x00007fffc854fec7 -[NSView _layoutSubtreeWithOldSize:] + 1215
      33  AppKit                              0x00007fffc854fec7 -[NSView _layoutSubtreeWithOldSize:] + 1215
      34  AppKit                              0x00007fffc854fec7 -[NSView _layoutSubtreeWithOldSize:] + 1215
      35  AppKit                              0x00007fffc8d1fe89 -[NSView _layoutSubtreeIfNeededAndAllowTemporaryEngine:] + 1517
      36  AppKit                              0x00007fffc856e3f1 -[NSWindow(NSConstraintBasedLayout) _layoutViewTree] + 163
      37  AppKit                              0x00007fffc85dd52a -[NSWindow(NSConstraintBasedLayout) layoutIfNeeded] + 269
      38  AppKit                              0x00007fffc8d49f78 _NSWindowGetDisplayCycleObserver_block_invoke.6213 + 67
      39  AppKit                              0x00007fffc85eec89 _37+[NSDisplayCycle currentDisplayCycle]_block_invoke + 454
      40  QuartzCore                          0x00007fffd045a1ba _ZN2CA11Transaction19run_commit_handlersE18CATransactionPhase + 46
      41  QuartzCore                          0x00007fffd0561e10 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 160
      42  QuartzCore                          0x00007fffd0458d55 _ZN2CA11Transaction6commitEv + 475
      43  AppKit                              0x00007fffc88d0d99 _37+[NSDisplayCycle currentDisplayCycle]_block_invoke.31 + 323
      44  CoreFoundation                      0x00007fffca92dec7 _CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
      45  CoreFoundation                      0x00007fffca92de37 _CFRunLoopDoObservers + 391
      46  CoreFoundation                      0x00007fffca90ed79 _CFRunLoopRun + 873
      47  CoreFoundation                      0x00007fffca90e7b4 CFRunLoopRunSpecific + 420
      48  HIToolbox                           0x00007fffc9e9e2ac RunCurrentEventLoopInMode + 240
      49  HIToolbox                           0x00007fffc9e9e0e1 ReceiveNextEventCommon + 432
      50  HIToolbox                           0x00007fffc9e9df16 _BlockUntilNextEventMatchingListInModeWithFilter + 71
      51  AppKit                              0x00007fffc84a06cd _DPSNextEvent + 1093
      52  AppKit                              0x00007fffc8c17830 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2730
      53  AppKit                              0x00007fffc849506d -[NSApplication run] + 926
      54  AppKit                              0x00007fffc845f983 NSApplicationMain + 1237
      55  ???                                 0x000000010c7cc9d2 0x0 + 4504472018
      56  ???                                 0x000000010c7cc5cd 0x0 + 4504470989
      57  macplastic                          0x0000000107d9a340 mono_jit_runtime_invoke + 2272
      58  macplastic                          0x0000000107e4b3c8 do_runtime_invoke + 88
      59  macplastic                          0x0000000107e4d8a0 mono_runtime_exec_main + 896
      60  macplastic                          0x0000000107e4d45d mono_runtime_run_main + 909
      61  macplastic                          0x0000000107cefca7 mono_jit_exec + 247
      62  macplastic                          0x0000000107cf2022 mono_main + 8098
      63  macplastic                          0x0000000107c982c4 xamarin_main + 980
      64  macplastic                          0x0000000107c98fd4 main + 36
      65  macplastic                          0x0000000107c7f944 start + 52
      66  ???                                 0x0000000000000002 0x0 + 2
  )
  libc++abi.dylib: terminating with uncaught exception of type NSException
  </code></pre>
  
  <p>Any help?</p>
  
  <p>Also, why does the app crash even if you add a try/catch block?? Is there any way to avoid the crash??</p>
  
  <pre><code>        try
          {
              NSAlert alert = new NSAlert();
              alert.InformativeText = message;
  
              alert.RunModal();
          }
          catch (Exception ex)
          {
  
           //Nothing to do, we did our best to show the error message, avoid an unhandled crash.
          }
  </code></pre>
  
  <p>Any help would be much appreciated.</p>