PDA

View Full Version : QSqlsocket crash when deleting



Higestromm
21st April 2017, 14:11
Hi,

I'm trying to use QSqlSocket in my application but when I delete it (manually or not), I got an exception in QSaredData line 165 :

inline ~QExplicitlySharedDataPointer() { if (d && !d->ref.deref()) delete d; }

I just wrote a minimal code here :

here is test.h

#include <QObject>
#include <QtNetwork/QSslSocket>

class test : public QObject
{
Q_OBJECT

public:
test();
~test();

private:
QSslSocket *socket;
};

here is test.cpp

test::test() :
QObject(NULL)
{
socket = new QSslSocket(this);
}

test::~test()
{
delete socket;
// I also try this but don't work too
// socket->deleteLater();
}

Here is the main program :

int main(int argc, char *argv[])
{
QApplication application(argc, argv);

test t;
}

Can you help me please ?

Thank you

d_stranz
21st April 2017, 21:42
You have created the QSslSocket instance as a child of your "test" class instance (by passing "this" as an argument to the QSslSocket constructor call in line 4 of test.cpp). This gives ownership of the QSslSocket instance to the instance of "test". Because of this, the "test" class instance will delete the socket itself when it is destructed. By calling delete yourself in the test destructor, you are basically trying to delete a socket instance that has already been deleted by the QObject base class destructor.

Higestromm
24th April 2017, 10:39
Hi, thank you for your answer.

Ok I understand but If I do not associate the Socket to a Q_Object, the program is still crashing when deleting it :/

Here is an example (I don't think I can do a code more small ;)):


#include "QSslSocket"

int main(int argc, char *argv[])
{

QSslSocket socket;
}

In this case, i got exactly the same error when the program close and delete the socket.
In this case, the socket is not link to anything and I don't call the detructor myself.

It's very stange :/

Higestromm
25th April 2017, 14:15
Ok, I've tried to use another version of QT.

Was using Qt 5.8.0 x86 library => Crash
If using 5.7.1 x64 library => No Crash

Very strange :/

Lesiok
25th April 2017, 15:48
Qt 5.8.0 with VS 2015 is working (no crash).