stlab / libraries

ASL libraries will be migrated here in the stlab namespace, new libraries will be created here.
https://stlab.cc
Boost Software License 1.0
660 stars 65 forks source link

Problem with main_executor for Qt on application exit #515

Open magpier84 opened 1 year ago

magpier84 commented 1 year ago

There is a crash on the application exit with main_executor for Qt. Solved by checking application existence. Or there is any way to cancel the task?

    void operator()(F f) const {
        // --> The existence of the application instance should be checked
        if (auto* app = QCoreApplication::instance()) {
            auto event = std::make_unique<executor_event>();
            event->set_task(std::move(f));
            auto receiver = event->receiver();
            QCoreApplication::postEvent(receiver, event.release());
        }
    }
magpier84 commented 1 year ago

Looks like problem with default_executor (on version 1.6.2)

sean-parent commented 1 year ago

Is it an assert? If you are using default_executor requires a call to pre_exit() prior to exiting the application. https://stlab.cc/libraries/concurrency/default_executor.hpp/

magpier84 commented 1 year ago

Sorry, a little bit details here:

  1. Platform macOS (libdispatch)
  2. I'm using serial_queue
  3. queue([](){...}).then(main_executor([](){...});
  4. I see path through default_executor in stack trace
  5. The problem is in c-tor of executor_event, application instance is NULL
        executor_event() : QEvent(QEvent::User), _receiver(new event_receiver()) {
            _receiver->moveToThread(QCoreApplication::instance()->thread());
        }

    So, the issue can be solved either by checking the application's existence or preventing its deletion. For the second case, it seems pre_exit should help, right?