From 76819c0316c03d1333d8e8c93bc7a3d8df4891ea Mon Sep 17 00:00:00 2001 From: Brett Stottlemyer Date: Sat, 15 Dec 2018 07:42:46 -0500 Subject: Convert QNX backend to use shared pointers Having destruction tied to parent could lead to double delete issues. Change-Id: Ia8787d96a1cbf9ddf6c06c50e8d59522bb10e420 Reviewed-by: Michael Brasser --- src/remoteobjects/qconnection_qnx_backend.cpp | 9 +++-- src/remoteobjects/qconnection_qnx_backend_p.h | 4 +-- src/remoteobjects/qconnection_qnx_server.cpp | 49 +++++++++++++-------------- src/remoteobjects/qconnection_qnx_server.h | 2 +- src/remoteobjects/qconnection_qnx_server_p.h | 5 +-- 5 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/remoteobjects/qconnection_qnx_backend.cpp b/src/remoteobjects/qconnection_qnx_backend.cpp index 5a393aa..d9bd9c5 100644 --- a/src/remoteobjects/qconnection_qnx_backend.cpp +++ b/src/remoteobjects/qconnection_qnx_backend.cpp @@ -118,17 +118,16 @@ void QnxClientIo::onStateChanged(QAbstractSocket::SocketState state) initializeDataStream(); } -QnxServerIo::QnxServerIo(QIOQnxSource *conn, QObject *parent) +QnxServerIo::QnxServerIo(QSharedPointer conn, QObject *parent) : ServerIoDevice(parent), m_connection(conn) { - m_connection->setParent(this); - connect(conn, &QIODevice::readyRead, this, &ServerIoDevice::readyRead); - connect(conn, &QIOQnxSource::disconnected, this, &ServerIoDevice::disconnected); + connect(conn.data(), &QIODevice::readyRead, this, &ServerIoDevice::readyRead); + connect(conn.data(), &QIOQnxSource::disconnected, this, &ServerIoDevice::disconnected); } QIODevice *QnxServerIo::connection() const { - return m_connection; + return m_connection.data(); } void QnxServerIo::doClose() diff --git a/src/remoteobjects/qconnection_qnx_backend_p.h b/src/remoteobjects/qconnection_qnx_backend_p.h index b44b783..19cb077 100644 --- a/src/remoteobjects/qconnection_qnx_backend_p.h +++ b/src/remoteobjects/qconnection_qnx_backend_p.h @@ -119,7 +119,7 @@ private: class QnxServerIo final : public ServerIoDevice { public: - explicit QnxServerIo(QIOQnxSource *conn, QObject *parent = nullptr); + explicit QnxServerIo(QSharedPointer conn, QObject *parent = nullptr); QIODevice *connection() const override; protected: @@ -127,7 +127,7 @@ protected: private: //TODO Source or Replica - QIOQnxSource *m_connection; + QSharedPointer m_connection; }; class QnxServerImpl final : public QConnectionAbstractServer diff --git a/src/remoteobjects/qconnection_qnx_server.cpp b/src/remoteobjects/qconnection_qnx_server.cpp index 34e1959..97b29c8 100644 --- a/src/remoteobjects/qconnection_qnx_server.cpp +++ b/src/remoteobjects/qconnection_qnx_server.cpp @@ -99,14 +99,13 @@ bool QQnxNativeServer::listen(const QString &name) return true; } -QIOQnxSource *QQnxNativeServer::nextPendingConnection() +QSharedPointer QQnxNativeServer::nextPendingConnection() { Q_D(QQnxNativeServer); d->mutex.lock(); Q_ASSERT(d->pending.length() > 0); - QIOQnxSource *io = d->pending.takeFirst(); + auto io = d->pending.takeFirst(); d->mutex.unlock(); - io->setParent(this); return io; } @@ -170,7 +169,7 @@ void QQnxNativeServerPrivate::thread_func() Q_FOREACH (int coid, coids) { const uint64_t uid = static_cast(scoid) << 32 | static_cast(coid); - QIOQnxSource *io = NULL; + QSharedPointer io; mutex.lock(); if (sources.contains(uid)) io = sources.take(uid); @@ -181,10 +180,9 @@ void QQnxNativeServerPrivate::thread_func() ham_action_handle_free(action); #endif - if (io) { + if (!io.isNull()) { io->d_func()->m_serverClosing.ref(); - QMetaObject::invokeMethod(io,"onDisconnected",Qt::QueuedConnection); - io->deleteLater(); + QMetaObject::invokeMethod(io.data(),"onDisconnected",Qt::QueuedConnection); } } @@ -205,7 +203,7 @@ void QQnxNativeServerPrivate::thread_func() const int scoid = recv_buf.pulse.scoid; if (connections.value(scoid).contains(coid)) connections[scoid].remove(coid); - QIOQnxSource *io = NULL; + QSharedPointer io; const uint64_t uid = static_cast(scoid) << 32 | static_cast(coid); mutex.lock(); if (sources.contains(uid)) @@ -217,10 +215,9 @@ void QQnxNativeServerPrivate::thread_func() ham_action_handle_free(action); #endif - if (io) { + if (!io.isNull()) { io->d_func()->m_serverClosing.ref(); - QMetaObject::invokeMethod(io,"onDisconnected",Qt::QueuedConnection); - io->deleteLater(); + QMetaObject::invokeMethod(io.data(),"onDisconnected",Qt::QueuedConnection); } qCDebug(QT_REMOTEOBJECT) << "Connection dropped" << coid; } @@ -286,7 +283,7 @@ void QQnxNativeServerPrivate::thread_func() const uint64_t uid = static_cast(msg_info.scoid) << 32 | static_cast(msg_info.coid); mutex.lock(); Q_ASSERT(sources.contains(uid)); - QIOQnxSource *io = sources.value(uid); + auto io = sources.value(uid); mutex.unlock(); io->d_func()->obLock.lockForWrite(); //NAR (Not-An-Error) @@ -310,7 +307,7 @@ void QQnxNativeServerPrivate::thread_func() const uint64_t uid = static_cast(msg_info.scoid) << 32 | static_cast(msg_info.coid); mutex.lock(); Q_ASSERT(sources.contains(uid)); - QIOQnxSource *io = sources.value(uid); + auto io = sources.value(uid); mutex.unlock(); int len_taken = 0; @@ -347,7 +344,7 @@ void QQnxNativeServerPrivate::thread_func() const uint64_t uid = static_cast(msg_info.scoid) << 32 | static_cast(msg_info.coid); mutex.lock(); Q_ASSERT(sources.contains(uid)); - QIOQnxSource *io = sources.value(uid); + auto io = sources.value(uid); mutex.unlock(); //Long-lock, use buffer+memcpy if we run into trouble @@ -382,7 +379,7 @@ void QQnxNativeServerPrivate::thread_func() } } mutex.lock(); - Q_FOREACH (QIOQnxSource *io, sources) + for (auto io: sources) io->d_func()->m_serverClosing.ref(); mutex.unlock(); name_detach(attachStruct, 0); @@ -406,17 +403,20 @@ bool QQnxNativeServerPrivate::listen(const QString &name) void QQnxNativeServerPrivate::cleanupIOSource(QIOQnxSource *conn) { - QIOQnxSource *io = NULL; + QSharedPointer io; mutex.lock(); - const uint64_t uid = sources.key(conn, UINT64_MAX); - - if (uid != UINT64_MAX) { - io = sources.take(uid); - io->d_func()->m_serverClosing.ref(); + QHashIterator> i(sources); + while (i.hasNext()) { + i.next(); + if (i.value().data() == conn) + io = sources.take(i.key()); + break; } mutex.unlock(); - if (io) + if (!io.isNull()) { + io->d_func()->m_serverClosing.ref(); io->close(); + } } void QQnxNativeServerPrivate::teardownServer() @@ -440,10 +440,9 @@ void QQnxNativeServerPrivate::teardownServer() void QQnxNativeServerPrivate::createSource(int rcvid, uint64_t uid, pid_t toPid) { Q_Q(QQnxNativeServer); - QIOQnxSource *io = new QIOQnxSource(rcvid); + auto io = QSharedPointer(new QIOQnxSource(rcvid)); io->moveToThread(q->thread()); - io->setParent(q); - QObject::connect(io, &QIOQnxSource::aboutToClose, + QObject::connect(io.data(), &QIOQnxSource::aboutToClose, q, &QQnxNativeServer::onSourceClosed); QIOQnxSourcePrivate *iop = io->d_func(); diff --git a/src/remoteobjects/qconnection_qnx_server.h b/src/remoteobjects/qconnection_qnx_server.h index cf39d17..3b168b1 100644 --- a/src/remoteobjects/qconnection_qnx_server.h +++ b/src/remoteobjects/qconnection_qnx_server.h @@ -65,7 +65,7 @@ public: bool hasPendingConnections() const; bool isListening() const; bool listen(const QString &name); - QIOQnxSource *nextPendingConnection(); + QSharedPointer nextPendingConnection(); QString serverName() const; bool waitForNewConnection(int msec = 0, bool *timedOut = nullptr); diff --git a/src/remoteobjects/qconnection_qnx_server_p.h b/src/remoteobjects/qconnection_qnx_server_p.h index 8f0be3b..4e2bfe4 100644 --- a/src/remoteobjects/qconnection_qnx_server_p.h +++ b/src/remoteobjects/qconnection_qnx_server_p.h @@ -56,6 +56,7 @@ #include #include +#include QT_BEGIN_NAMESPACE @@ -94,8 +95,8 @@ public: QString serverName; name_attach_t *attachStruct; QHash > connections; - QHash sources; - QList pending; + QHash> sources; + QList> pending; QAtomicInt running; Thread thread; mutable QMutex mutex; -- cgit v1.2.3