Do not access QObject API before its private data is initialized.
authorMilian Wolff <mail@milianw.de>
Thu, 6 Feb 2014 16:30:55 +0000 (17:30 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 6 Feb 2014 19:17:10 +0000 (20:17 +0100)
Inside the ctor of the private data, the QObject has not yet
been associated with the data. Thus, accessing it indirectly i.e.
to setup private signal/slot connections, will trigger crashes.

Now we delay accessing this API and call an init() function manually
in the parent QObject-inheriting class.

Change-Id: I2c3ce7335c54d42af6bce87de867ee0ef197efc6
Reviewed-by: Kurt Pattyn <pattyn.kurt@gmail.com>
src/websockets/qwebsocket.cpp
src/websockets/qwebsocket_p.cpp
src/websockets/qwebsocketserver.cpp
src/websockets/qwebsocketserver_p.cpp
src/websockets/qwebsocketserver_p.h

index 45394e3..6e60230 100644 (file)
@@ -262,6 +262,8 @@ QWebSocket::QWebSocket(const QString &origin,
                        QObject *parent) :
     QObject(*(new QWebSocketPrivate(origin, version, this)), parent)
 {
+    Q_D(QWebSocket);
+    d->init();
 }
 
 /*!
@@ -301,6 +303,8 @@ QWebSocket::QWebSocket(QTcpSocket *pTcpSocket,
                        QWebSocketProtocol::Version version, QObject *parent) :
     QObject(*(new QWebSocketPrivate(pTcpSocket, version, this)), parent)
 {
+    Q_D(QWebSocket);
+    d->init();
 }
 
 /*!
index fe4dd8a..5d58fec 100644 (file)
@@ -111,7 +111,6 @@ QWebSocketPrivate::QWebSocketPrivate(const QString &origin, QWebSocketProtocol::
     m_dataProcessor(),
     m_configuration()
 {
-    init();
 }
 
 /*!
@@ -142,8 +141,6 @@ QWebSocketPrivate::QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol:
     m_dataProcessor(),
     m_configuration()
 {
-    init();
-    makeConnections(m_pSocket.data());
 }
 
 /*!
@@ -154,6 +151,10 @@ void QWebSocketPrivate::init()
     Q_ASSERT(q_ptr);
     //TODO: need a better randomizer
     qsrand(static_cast<uint>(QDateTime::currentMSecsSinceEpoch()));
+
+    if (m_pSocket) {
+        makeConnections(m_pSocket.data());
+    }
 }
 
 /*!
index 10ee83e..a8a25be 100644 (file)
@@ -236,6 +236,8 @@ QWebSocketServer::QWebSocketServer(const QString &serverName, SslMode secureMode
 #ifdef QT_NO_SSL
     Q_UNUSED(secureMode)
 #endif
+    Q_D(QWebSocketServer);
+    d->init();
 }
 
 /*!
index 912c66e..6797cbc 100644 (file)
@@ -73,6 +73,13 @@ QWebSocketServerPrivate::QWebSocketServerPrivate(const QString &serverName,
     m_errorString()
 {
     Q_ASSERT(pWebSocketServer);
+}
+
+/*!
+    \internal
+ */
+void QWebSocketServerPrivate::init()
+{
     if (m_secureMode == NonSecureMode) {
         m_pTcpServer = new QTcpServer();
         if (Q_LIKELY(m_pTcpServer))
index a9aa880..c4afd3d 100644 (file)
@@ -85,6 +85,7 @@ public:
                                      QWebSocketServer * const pWebSocketServer);
     virtual ~QWebSocketServerPrivate();
 
+    void init();
     void close();
     QString errorString() const;
     bool hasPendingConnections() const;