summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrett Stottlemyer <bstottle@ford.com>2018-12-15 07:42:46 -0500
committerBrett Stottlemyer <bstottle@ford.com>2018-12-19 00:17:33 +0000
commit76819c0316c03d1333d8e8c93bc7a3d8df4891ea (patch)
tree67a8293b5239eeaa28dc2eac123116f8bf302807
parent5bc5f04be23237f9d55b2134ae55bd93bed342a0 (diff)
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 <michael.brasser@live.com>
-rw-r--r--src/remoteobjects/qconnection_qnx_backend.cpp9
-rw-r--r--src/remoteobjects/qconnection_qnx_backend_p.h4
-rw-r--r--src/remoteobjects/qconnection_qnx_server.cpp49
-rw-r--r--src/remoteobjects/qconnection_qnx_server.h2
-rw-r--r--src/remoteobjects/qconnection_qnx_server_p.h5
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<QIOQnxSource> 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<QIOQnxSource> conn, QObject *parent = nullptr);
QIODevice *connection() const override;
protected:
@@ -127,7 +127,7 @@ protected:
private:
//TODO Source or Replica
- QIOQnxSource *m_connection;
+ QSharedPointer<QIOQnxSource> 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<QIOQnxSource> 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<uint64_t>(scoid) << 32 | static_cast<uint32_t>(coid);
- QIOQnxSource *io = NULL;
+ QSharedPointer<QIOQnxSource> 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<QIOQnxSource> io;
const uint64_t uid = static_cast<uint64_t>(scoid) << 32 | static_cast<uint32_t>(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<uint64_t>(msg_info.scoid) << 32 | static_cast<uint32_t>(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<uint64_t>(msg_info.scoid) << 32 | static_cast<uint32_t>(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<uint64_t>(msg_info.scoid) << 32 | static_cast<uint32_t>(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<QIOQnxSource> 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<uint64_t, QSharedPointer<QIOQnxSource>> 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<QIOQnxSource>(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<QIOQnxSource> 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 <QAtomicInt>
#include <QMutex>
+#include <QSharedPointer>
QT_BEGIN_NAMESPACE
@@ -94,8 +95,8 @@ public:
QString serverName;
name_attach_t *attachStruct;
QHash<int, QSet<int> > connections;
- QHash<uint64_t, QIOQnxSource *> sources;
- QList<QIOQnxSource *> pending;
+ QHash<uint64_t, QSharedPointer<QIOQnxSource>> sources;
+ QList<QSharedPointer<QIOQnxSource>> pending;
QAtomicInt running;
Thread<QQnxNativeServerPrivate> thread;
mutable QMutex mutex;