Use Qt thread correctly
-
QByteArray CAgilentLan::GetLastResponse(void) { QMutexLocker(m_MtxResponse); return m_ByteArrayRead; }
Won't that temporary QMutexLocker need a name? Otherwise it will be deconstructed as a temporary object at the end of the first statement and the return won't be locked properly.
@corruptedsyntax Sure, thanks for pointing that out :-)
QByteArray CAgilentLan::GetLastResponse(void) { QMutexLocker mutexLocker(m_MtxResponse); return m_ByteArrayRead; }
-
@corruptedsyntax Sure, thanks for pointing that out :-)
QByteArray CAgilentLan::GetLastResponse(void) { QMutexLocker mutexLocker(m_MtxResponse); return m_ByteArrayRead; }
-
@jsulm
Hi,
So is correct:QByteArray CAgilentLan::GetLastResponse(void) { QMutexLocker mutexLocker(&m_MtxResponse); return m_ByteArrayRead; }
@Galilio yes
-
wrote on 7 Jul 2017, 05:33 last edited by
Hi,
How can I avoid this error? -
wrote on 7 Jul 2017, 06:50 last edited by
@jsulm
Problem occurs when the application is exited.
The Destructor is then called, which looks like this:CAgilentLan::~CAgilentLan() { this->Close(GetiTimeOutClose()); if (this->Execute("DeleteObject", 0, 0, 0) != enRspOK) { throw(QString("Error AgilentLan.CPP/Destructor TcpSocket Delete memory error")); } }
And right here is the problem:
GetTcpSocket()->disconnectFromHost();
-
@jsulm
Problem occurs when the application is exited.
The Destructor is then called, which looks like this:CAgilentLan::~CAgilentLan() { this->Close(GetiTimeOutClose()); if (this->Execute("DeleteObject", 0, 0, 0) != enRspOK) { throw(QString("Error AgilentLan.CPP/Destructor TcpSocket Delete memory error")); } }
And right here is the problem:
GetTcpSocket()->disconnectFromHost();
@Galilio What error? What problem? Can you be more precise?
You should not throw exceptions from destructors. -
@Galilio What error? What problem? Can you be more precise?
You should not throw exceptions from destructors.wrote on 7 Jul 2017, 07:04 last edited by Galilio 7 Jul 2017, 07:04@jsulm
The TcpSocket connection can not be closed
Error is:ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread dc02d0. Receiver '' (of type 'QNativeSocketEngine') was created in thread 4912418", file kernel\qcoreapplication.cpp, line 541
-
@jsulm
The TcpSocket connection can not be closed
Error is:ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread dc02d0. Receiver '' (of type 'QNativeSocketEngine') was created in thread 4912418", file kernel\qcoreapplication.cpp, line 541
@Galilio You are doing something wrong with your threads
-
wrote on 7 Jul 2017, 08:41 last edited by
@jsulm
Yes but what?
My QThread looks like this:m_pvWorkerThread = new QThread(); m_pvAgilentWorker = new CAgilentLan(settings_l.strfuGetIP_DSO(), 500, settings_l.bofuGetSimulate()); m_pvAgilentWorker->moveToThread(m_pvWorkerThread); connect(m_pvWorkerThread, &QThread::started, m_pvAgilentWorker, &CAgilentLan::agilentStart); connect(m_pvWorkerThread, &QThread::finished, m_pvWorkerThread, &QThread::deleteLater); connect(m_pvWorkerThread, &QThread::finished, m_pvWorkerThread, &CAgilentLan::deleteLater); m_pvWorkerThread->start();
-
@jsulm
Yes but what?
My QThread looks like this:m_pvWorkerThread = new QThread(); m_pvAgilentWorker = new CAgilentLan(settings_l.strfuGetIP_DSO(), 500, settings_l.bofuGetSimulate()); m_pvAgilentWorker->moveToThread(m_pvWorkerThread); connect(m_pvWorkerThread, &QThread::started, m_pvAgilentWorker, &CAgilentLan::agilentStart); connect(m_pvWorkerThread, &QThread::finished, m_pvWorkerThread, &QThread::deleteLater); connect(m_pvWorkerThread, &QThread::finished, m_pvWorkerThread, &CAgilentLan::deleteLater); m_pvWorkerThread->start();
Lifetime Qt Championwrote on 7 Jul 2017, 08:44 last edited by jsulm 7 Jul 2017, 08:47@Galilio Sorry, currently I have no time to analyse your code.
You should read: https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
15/15