Skip to content

Commit

Permalink
cleanup viewer message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rectalogic committed Oct 7, 2023
1 parent 345d99d commit 1f682ad
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions viewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Viewer, Cleanup> 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;
}

0 comments on commit 1f682ad

Please sign in to comment.