QSharedPointer 在 emit 中被销毁

QSharedPointer gets destroyed within emit

本文关键字:emit QSharedPointer      更新时间:2023-10-16

我是Qt的新手,在信号内传递QSharedPointer遇到了一些问题。我正在使用两个线程(UI 和一个工作人员)。工作线程使用包含自定义 QObject 的 QSharedPointer 的信号向 UI 发送信号:

class MyObject : QObject {...}
class Window : public QWidget {
Q_OBJECT
public slots:
void onFound(QSharedPointer<MyObject>);
}
class Worker : public QObject {
Q_OBJECT
public signals:
void found(QSharedPointer<MyObject>);
}

我将工作人员found与窗口连接onFoundQt::QueuedConnection,因为它们位于不同的线程中,因此通信必须是异步的。

现在,当我传递引用我的对象的最后一个QSharedPointer时,我观察到以下行为:

  • 信号 moc 将引用我的指针投射到void*并对其进行弧形。
  • 该函数返回导致共享指针和相应的对象被销毁。

这不是我所期望的 - 尽管这是合理的。QSharedPointer 通常是否设计为以这种方式通过信号传递?如果是这样,是否有一种在排队时保留引用的机制?

我考虑了以下解决方案,但我对它们都不完全满意:

将引用保留
  1. 在排队时保留引用的位置。但是哪里是这样做的合理地方,我什么时候应该放手。
  2. Qt::DirectConnection建立连接,但随后我仍然必须以某种方式切换线程(与以前的情况相同)
  3. 引入一个带有std::function参数的新信号/插槽,用于传递要在目标线程中执行的 lambda 函数并捕获我的共享指针的副本。(这是我目前的解决方案,但它并不优雅,不是吗?

您还有其他建议或想法吗?

信号返回不会破坏相应的对象。QMetaObject::activate调用复制共享指针。以下是send信号的实现:

// SIGNAL 0
void IO::send(const QSharedPointer<Unique> & _t1)
{
void *_a[] = { nullptr, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 0, _a);
}

您可能正在经历一场争用:当发出信号的线程恢复执行时,目标线程已经收到了对象。因此,在发射线程中似乎对象已经消失了 - 因为到那时,它已经消失了。然而,目标对象可以很好地接收实例。它工作正常。

下面的示例说明了它在单线程和多线程情况下都有效,然后通过确保目标线程始终赢得争用来重现您的问题:

// https://github.com/KubaO/stackoverflown/tree/master/questions/shared-pointer-queued-49133331
#include <QtCore>
class Unique : public QObject {
Q_OBJECT
int const m_id = []{
static QAtomicInteger<int> ctr;
return ctr.fetchAndAddOrdered(1);
}();
public:
int id() const { return m_id; }
};
class IO : public QObject {
Q_OBJECT
int m_lastId = -1;
public:
Q_SIGNAL void send(const QSharedPointer<Unique> &);
Q_SLOT void receive(const QSharedPointer<Unique> & u) {
m_lastId = u->id();
}
int lastId() const { return m_lastId; }
};
int main(int argc, char ** argv) {
Q_ASSERT(QT_VERSION >= QT_VERSION_CHECK(5,9,0));
QCoreApplication app{argc, argv};
IO src, dst;
QObject::connect(&src, &IO::send, &dst, &IO::receive, Qt::QueuedConnection);
QSharedPointer<Unique> u;
QWeakPointer<Unique> alive;
int id = -1;
// Single-threaded case
alive = (u.reset(new Unique), u);
id = u->id();
Q_ASSERT(dst.lastId() != id); // the destination hasn't seen the object yet
emit src.send(u);
u.reset();
Q_ASSERT(!u);                 // we gave up ownership of the object
Q_ASSERT(dst.lastId() != id); // the destination mustn't seen the object yet
Q_ASSERT(alive);              // the object must be still alive
app.processEvents();
Q_ASSERT(dst.lastId() == id); // the destination must have seen the object now
Q_ASSERT(!alive);             // the object should have been destroyed by now
// Multi-threaded setup
struct Thread : QThread { ~Thread() { quit(); wait(); } } worker;
worker.start();
dst.moveToThread(&worker);
QSemaphore s_src, s_dst;
// This thread wins the race
alive = (u.reset(new Unique), u);
id = u->id();
Q_ASSERT(dst.lastId() != id);
QTimer::singleShot(0, &dst, [&]{ s_src.release(); s_dst.acquire(); });
// stop the thread
s_src.acquire();              // wait for thread to be stopped
emit src.send(u);
QTimer::singleShot(0, &dst, [&]{ s_src.release(); });
// resume the main thread when done
u.reset();
Q_ASSERT(!u);
Q_ASSERT(alive);              // we won the race: the object must be still alive
s_dst.release();              // get the thread running
s_src.acquire();              // wait for the thread to be done
Q_ASSERT(dst.lastId() == id);
Q_ASSERT(!alive);
// The other thread wins the race
alive = (u.reset(new Unique), u);
id = u->id();
Q_ASSERT(dst.lastId() != id);
emit src.send(u);
QTimer::singleShot(0, &dst, [&]{ s_src.release(); });
// resume the main thread when done
u.reset();
s_src.acquire();             // wait for worker thread to be done
Q_ASSERT(!u);
Q_ASSERT(!alive);            // we lost the race: the object must be gone
Q_ASSERT(dst.lastId() == id); // yet the destination has received it!
// Ensure the rendezvous logic didn't mess up
Q_ASSERT(id == 2);
Q_ASSERT(!s_src.available());
Q_ASSERT(!s_dst.available());
}
#include "main.moc"