diff --git a/viewer/main.cpp b/viewer/main.cpp index 6358d5d..4eeb9e1 100644 --- a/viewer/main.cpp +++ b/viewer/main.cpp @@ -18,30 +18,23 @@ int main(int argc, char* argv[]) app.setOrganizationName("WebVfx"); app.setApplicationName("WebVfx Viewer"); + Viewer viewer; + static Viewer* viewerPtr = &viewer; // Can't use a capturing lambda as a function pointer // https://stackoverflow.com/questions/73366933/what-does-mean-in-cpp-lambda-declaration-auto-fun1 - // Need to ensure viewer is destroyed before we leave main and exit, - // otherwise it's destructor tries to clean up global state which is in the unloaded webvfx library - - struct Cleanup { - static inline void cleanup(Viewer* pointer) - { - qInstallMessageHandler(0); - delete pointer; - } - }; - static Viewer* viewer = new Viewer(); - QScopedPointer destroyer(viewer); - auto messageHandler = +[](QtMsgType type, const QMessageLogContext& context, const QString& msg) mutable { - viewer->messageHandler(type, context, msg); + if (viewerPtr) + viewerPtr->messageHandler(type, context, msg); }; qInstallMessageHandler(messageHandler); - viewer->show(); + viewer.show(); QStringList args(QApplication::arguments()); if (args.size() > 1) - viewer->loadFile(args.at(1)); + viewer.loadFile(args.at(1)); - return app.exec(); + auto result = app.exec(); + qInstallMessageHandler(0); + viewerPtr = nullptr; + return result; }