From 772efa65ab5d74767fb24f788004d6ed351984cf Mon Sep 17 00:00:00 2001 From: Romain Janvier Date: Sat, 19 Sep 2026 22:19:43 +0200 Subject: [PATCH] Fix console crash on macOS ConsoleWrapper constructor move the lambda. As as result it's unsafe to capture 'this' because the instance of this is eventually destroyed when it goes out of scope (it's a "temporary"). Don't know why it was unotticed on Linux/Windows. The PR removes also a usesless member and fixes a constructor (QColor passed but not used tho init the brush) --- src/Runtime/Consoles.h | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Runtime/Consoles.h b/src/Runtime/Consoles.h index 0a07ab32..25fb250a 100644 --- a/src/Runtime/Consoles.h +++ b/src/Runtime/Consoles.h @@ -18,6 +18,7 @@ #ifndef PYTHON_PLUGIN_CONSOLES_H #define PYTHON_PLUGIN_CONSOLES_H +#include #include #include @@ -107,27 +108,30 @@ class ccConsoleOutput private: const QString m_prefix; - ConsoleWrapper m_output{[this](const QString &message) + /// The callback must capture the values it needs, not "this": + /// because instances of this class are moved by pybind11... + ConsoleWrapper m_output{[prefix = m_prefix](const QString &message) { - if (m_prefix.isEmpty()) + if (prefix.isEmpty()) { ccLog::Print(message); } else { - ccLog::Print(m_prefix + message); + ccLog::Print(prefix + message); } }}; }; /// Writes messages to the QListWidget given +/// class ListWidgetConsole { public: ListWidgetConsole(QListWidget *view, const Qt::GlobalColor color) : m_view(view), m_brush(color) { } - ListWidgetConsole(QListWidget *view, const QColor &color) : m_view(view), m_brush() {} + ListWidgetConsole(QListWidget *view, const QColor &color) : m_view(view), m_brush(color) {} explicit ListWidgetConsole(QListWidget *view) : m_view(view), m_brush() {} void write(const char *messagePart) @@ -147,12 +151,13 @@ class ListWidgetConsole private: QListWidget *m_view; QBrush m_brush; - QString m_prefix; - ConsoleWrapper m_output{[this](const QString &message) + /// callback must capture the values it needs, not "this". + /// because instances of this class are moved by pybind11... + ConsoleWrapper m_output{[view = m_view, brush = m_brush](const QString &message) { auto *messageItem = new QListWidgetItem(message); - messageItem->setForeground(m_brush); - m_view->addItem(messageItem); + messageItem->setForeground(brush); + view->addItem(messageItem); }}; }; #endif // PYTHON_PLUGIN_CONSOLES_H