diff --git a/src/messages/Image.cpp b/src/messages/Image.cpp index e189d54be..6f5171a24 100644 --- a/src/messages/Image.cpp +++ b/src/messages/Image.cpp @@ -278,7 +278,10 @@ Image::~Image() return; } - // run destructor of Frames in gui thread + // Ensure the destructor for our frames is called in the GUI thread + // If the Image destructor is called outside of the GUI thread, move the + // ownership of the frames to the GUI thread, otherwise the frames will be + // destructed as part as we go out of scope if (!isGuiThread()) { postToThread([frames = this->frames_.release()]() { diff --git a/src/util/PostToThread.hpp b/src/util/PostToThread.hpp index 5f90849d7..45d715a1d 100644 --- a/src/util/PostToThread.hpp +++ b/src/util/PostToThread.hpp @@ -1,7 +1,8 @@ #pragma once -#include +#include "debug/AssertInGuiThread.hpp" +#include #include #include @@ -29,6 +30,19 @@ private: std::function action_; }; +template +static void runInGuiThread(F &&fun) +{ + if (isGuiThread()) + { + fun(); + } + else + { + postToThread(fun); + } +} + // Taken from // https://stackoverflow.com/questions/21646467/how-to-execute-a-functor-or-a-lambda-in-a-given-thread-in-qt-gcd-style // Qt 5/4 - preferred, has least allocations