How to safely access Network in a thread pool? - c++

I have the following code which is used to access the network using a thread pool(aka, the following code might be called by several different threads at the same time.)
...
QEventLoop q;
QNetworkReply *reply;
QNetworkRequest qheader;
QNetworkAccessManager network;
QTimer tT;
tT.setSingleShot(true);
connect(&network, SIGNAL(finished(QNetworkReply*)),
&q, SLOT(quit()));
connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
network.setProxy(Proxy);
QString url=MakeImageUrl(type,pos,zoom,LanguageStr);
qheader.setUrl(QUrl(url));
qheader.setRawHeader("User-Agent",UserAgent);
qheader.setRawHeader("Accept","*/*");
reply=network.get(qheader);
ret=reply->readAll();
reply->deleteLater();
...
Then the program some times crash at the reply = network.get(qheader), am I doing anything wrong here? I have tried qt5.4.1 and qt4.8.6, though the callstack of the crash is not exactly the same, I just wonder how correct is the above code?

Related

QNetworkReply throwing SIGSEGV when finished signal emitted

My application makes use of QNetworkReply's for send and receiving data from a RESTful API.
There are many tutorials available for using the QNetworkReply with QNetworkAccessManager
Once such example which I used can be found here or even here
Basic Usage:
// Header
QNetworkAccessManager *manager;
QNetworkReply *myReply;
QMetaObject::Connection conReply;
// Making the request
void MainWindow::makeRequest(QString url) {
//...
QNetworkRequest request(QUrl(url));
myReply = manager->get(request) // or post(request)
conReply = QObject::connect(myReply, SIGNAL(finished()), this, SLOT(myReplyResponse()));
}
// Handling the request
void MainWindow::myReplyResponse(){
QObject::disconnect(conReply);
QByteArray data = myReply->readAll();
// or QByteArray data = myReply->read(myReply->bytesAvailable());
myReply->deleteLater();
// do something with this data
//...
}
Using a similar implementation, I request data every X seconds.
Problem:
When receiving the finished() signal, the code handling the reply is triggered, but when reading the data, I get a SIGSEGV.
This issue seems to occur at random, thus I cannot determine what triggers it.
Any suggestions would be gladly accepted.
What is probably happening is that it is delaying the order, let's say that an order is sent every second but it takes 2 seconds to replicate, after 2 seconds you have read the reply and you have deleted it from memory, when the other comes myReply is an empty pointer. What you must do is use sender() to obtain the replica, and it is always advisable to validate that you do not have an empty pointer:
*.h
private:
QNetworkAccessManager *manager;
*.cpp
[...]
manager = new QNetworkAccessManager(this);
[...]
void MainWindow::makeRequest(const QString &url)
{
Qurl mUrl(url);
QNetworkRequest request(mUrl);
QNetworkReply *myReply = manager->get(request); // or post(request)
connect(myReply, &QNetworkReply::finished, this, &MainWindow::myReplyResponse);
}
void MainWindow::myReplyResponse()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
if(reply){
QByteArray data = reply->readAll();
qDebug()<<data;
reply->deleteLater();
}
}

QProgressDialog when setValue(0) is called

I have a class that is inherited from QProgressDialog. It sometimes crashes and its inside QProgressDialog class code.
I detected that when the setValue(0); is called in its constructor, crash happens. When I commented out this function, It does not crash. Even if setValue(0) is called after constructing of object crash still occurs.
This crash occurs when signals are emitted faster. Code piece shows how the function is called:
ProgressDialog progress(nullptr);
progress.setLabelText("Loading Result Files");
QFutureWatcher<void> watcher;
QFuture<void> future = QtConcurrent::run(myClassPtr,&myClass::myFunc);
QEventLoop loop;
QObject::connect(&watcher, SIGNAL(started()), &progress, SLOT(show()));
QObject::connect(&watcher, SIGNAL(finished()), &progress, SLOT(hide()));
QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
QObject::connect(transientAnimation, SIGNAL(progress(int)), &progress, SLOT(setValue(int)));
watcher.setFuture(future);
loop.exec();
progress signal is emitted inside myFunc() in another thread. In this function, multiple files are read in a folder and progress signal is emitted.
Why does this crash occur?
Thanks in advance.

QNetworkAccessManager cause the warning: QWaitCondition: destroyed while thread is still running

void QGCCore::getLatestGCSVersion()
{
QString url = "http://xxxxx";
QNetworkRequest newRequest(url);
newRequest.setUrl(url);
QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
networkManager->get(newRequest);
}
void QGCCore::downloadFinished(QNetworkReply * reply)
{
reply->deleteLater();
}
The above code, with empty constructor and destructor.
If I start the software and close it, it gives warning:
QWaitCondition: destroyed while thread is still running.
However, if I wait a while to close this, then it is Ok...
What's going wrong here?

Memory leak in QTcpServer attending connections in different thread

I'm developing a RPC server in Qt 4.7 on Windows 7.
In order to attend several executions at the same time every request run in a separate thread (as functions might be blocking). I inherited from QTcpServer and reimplemented the incomingConnection function, it looks like this:
void RpcServer::incomingConnection(int socketDescriptor){
QThread *thread = new QThread();
RpcServerConnection *client = new RpcServerConnection(socketDescriptor);
client->moveToThread(thread);
connect(thread, SIGNAL(started()), client, SLOT(init()));
connect(client, SIGNAL(finish()), thread, SLOT(quit()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
RpcServerConnection managed data exchange. The init method looks like this:
void RpcServerConnection::init(){
qDebug() << "ServerSocket(" << QThread::currentThreadId() << "): Init";
clientConnection = new QTcpSocket();
clientConnection->setSocketDescriptor(socketDescriptor);
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readFromSocket()));
connect(clientConnection, SIGNAL(disconnected()), this, SLOT(deleteLater()));
connect(this, SIGNAL(finish()), this, SLOT(deleteLater()));
}
Once all data is received and response is sent, finish signal is emited.
Debugging I can see that all threads and sockets are deleted. However, process memory increases with every new connection and it is not freed when it ends...
Do I have to free anything else when inheriting from QTcpServer?
The problem may be with a race/undefined calling order. RpcServerConnection::finish() is connected both to its deleteLater() slot, and to the thread's quit() slot. If the thread's quit slot is entered first, the thread will terminate immediately from the event loop, before anything can be done about the deferred deletion.
Instead of:
connect(client, SIGNAL(finish()), thread, SLOT(quit()));
try:
connect(client, SIGNAL(destroyed()), thread, SLOT(quit()));

How to tell QThread to wait until work is done ,and then finish?

I have a simple application that uses one worker thread.
This worker thread is started and initializes DownloadManager, which is responsible for downloading files from the net.
In my main application class I have the finished() SIGNAL on the thread that is emitted before the DownloadManager finishes.
My question is how to make the worker thread wait until the DownloadManager finishes its work.
Here is example code :
class Main
{
m_DownloadWorker = new DownloadWorker(this);
QObject::connect(pm_hotosDownloadWorker, SIGNAL(finished()), this, SLOT(DownloadWorkerFinished()));
m_DownloadWorker->Execute();
// do i need to do here something so the thread will wait ?
.....
void Main::DownloadWorkerFinished()
{
Log("DownloadWorkerFinished");
}
};
class DownloadWorker : public QThread
{
void DownloadWorker::Execute()
{
// do i need to do here somthing so the thread will wait ?
start();
}
void DownloadWorker::run()
{
// do i need to do here somthing so the thread will wait ?
DownloadManager* pDownloadManager = new DownloadManager(this);
pDownloadManager->download();
}
};
class DownloadManager: public QObject
{
// downloading stuff using Qt networkmanager
.....
.....
}
In cases when you have a signal that is emitted when an asynchronous operation is completed, you can always use QEventLoop to "turn" the asynchronous operation into synchronous with the current thread. It is still asynchronous, but the thread will "wait" for it to finish.
QNetworkAccessManager nam;
QEventLoop loop;
QNetworkReply *reply = nam.get(request);
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
So basically you put this in you DownloadManager class where you want to do a network request synchronously. exec() will return once the loop's quit slot has been called.
You can use QThread::exec() call to run your thread in the event loop. The thread will run it until you tell your thread to exit by calling QThread::exit(). So some sample code can look like this:
void DownloadWorker::run()
{
DownloadManager* pDownloadManager = new DownloadManager(this);
connect(pDownloadManager, SIGNAL(finished()), SLOT(exit()));
connect(pDownloadManager, SIGNAL(error()), SLOT(exit()));
pDownloadManager->download();
exec();
}
That would guarantee you that your thread won't quit until the "finished()" signal of your DownloadManager is issued.
Note: Here I put an example of how to solve your problem but I don't know your whole app code. That means there is not guarantee this code is thread safe and consistent. You need to take care of the mutexes and all the correct synchronization yourself. Be very careful ! Working with such a "low level" thread API requites good understanding of multithereading.
Hope that helps