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 <jared@nomic.ai>
This commit is contained in:
Jared Van Bortel 2025-02-04 13:09:21 -05:00
parent 17bbedc77c
commit 82e543e6b9

View File

@ -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.