From 82e543e6b9011bbba21da212c717943920b33e9d Mon Sep 17 00:00:00 2001 From: Jared Van Bortel Date: Tue, 4 Feb 2025 13:09:21 -0500 Subject: [PATCH] chatmodel: fix call of setParent from wrong thread Attempting to notify the objects of this change from a different thread causes an error in debug builds: ``` ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a a different thread. Current thread 0x0x1a9dc360490. Receiver '' (of type 'ChatModel') was created in thread 0x0x1a9ad1a99f0", file C:\Users\qt\work\qtbase\src\corelib\kernel\qcoreapplication.cpp, line 557 ``` QObject::setParent is not documented to be thread-safe. Signed-off-by: Jared Van Bortel --- gpt4all-chat/src/chatmodel.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gpt4all-chat/src/chatmodel.h b/gpt4all-chat/src/chatmodel.h index a340b0ca..27e6063a 100644 --- a/gpt4all-chat/src/chatmodel.h +++ b/gpt4all-chat/src/chatmodel.h @@ -204,7 +204,8 @@ public: : QObject(nullptr) { moveToThread(parent->thread()); - setParent(parent); + // setParent must be called from the thread the object lives in + QMetaObject::invokeMethod(this, [this, parent]() { this->setParent(parent); }); } // NOTE: System messages are currently never serialized and only *stored* by the local server.