stevenlovegrove / Pangolin

Pangolin is a lightweight portable rapid development library for managing OpenGL display / interaction and abstracting video input.
MIT License
2.36k stars 852 forks source link

Exception raised in macOS due to nextEventMatchingMask being called outside main thread | M1/M2 MacOS #876

Closed amandeepgini closed 1 year ago

amandeepgini commented 1 year ago

Hello,

I'm currently using the Pangolin library as part of an ORB_SLAM2 project on macOS. When I run my application, it terminates with an NSInternalInconsistencyException, stating that nextEventMatchingMask should only be called from the Main Thread!

Here is the stack trace for the exception:

Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘nextEventMatchingMask should only be called from the Main Thread!’
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000199a0f154 __exceptionPreprocess + 176
    1   libobjc.A.dylib                     0x000000019952e4d4 objc_exception_throw + 60
    2   AppKit                              0x000000019cbb5730 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2844
    3   libpango_windowing.dylib            0x00000001045953c0 +[PangolinNSApplication run_step] + 56
    4   libpango_windowing.dylib            0x00000001045905cc _ZN8pangolin9OsxWindowC2ERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEiibj + 160
    5   libpango_windowing.dylib            0x0000000104593f5c _ZZN8pangolin24RegisterOsxWindowFactoryEvEN16OsxWindowFactory4OpenERKNS_3UriE + 1420
    6   libpango_windowing.dylib            0x000000010459684c _ZN8pangolin15FactoryRegistry9ConstructINS_15WindowInterfaceEEENSt3__110unique_ptrIT_NS3_14default_deleteIS5_EEEERKNS_3UriE + 1056
    7   libpango_windowing.dylib            0x00000001045963c8 _ZN8pangolin15ConstructWindowERKNS_3UriE + 52
    8   libpango_display.dylib              0x0000000104771e28 _ZN8pangolin19CreateWindowAndBindENSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEEiiRKNS_6ParamsE + 2176
    9   libORB_SLAM2.dylib                  0x00000001030c2404 _ZN9ORB_SLAM26Viewer3RunEv + 108
    10  libORB_SLAM2.dylib                  0x0000000103037594 _ZNSt3__114__thread_proxyB7v160006INS_5tupleIJNS_10unique_ptrINS_15__thread_structENS_14default_deleteIS3_EEEEMN9ORB_SLAM26ViewerEFvvEPS8_EEEEEPvSD_ + 72
    11  libsystem_pthread.dylib             0x00000001998b7fa8 _pthread_start + 148
    12  libsystem_pthread.dylib             0x00000001998b2da0 thread_start + 8
)
libc++abi: terminating due to uncaught exception of type NSException
amandeepgini commented 1 year ago

Looks like macOS enforces that all UI-related operations be performed in the main thread. This is because the main thread is the only thread that has access to the user interface. This could be the problem.

amandeepgini commented 1 year ago

It is not the issue with Pangolin Library, it is the issue with MacOS. On MacOS, UI-related operations have to be performed in the main thread. This is because the main thread is the only thread that has access to the user interface.

https://stackoverflow.com/questions/74893322/why-does-macos-ios-force-the-main-thread-to-be-the-ui-thread-and-are-there-an