desktop app crashed when 2 event loops are used
-
I am trying to write some data to an url using get request from QNetworkAccessManager class. I am creating an event loop in it. There is another event loop running in the main thread which is the main event loop for the application. The application is getting crashed when it reaches eventLoop.exec(); line in the code below. Any idea why is it happening??
{ SharedPtr<QNetworkAccessManager> networkAccessManager; networkAccessManager(new QNetworkAccessManager()); bool res = write(request); } bool Write(QNetworkRequest request) const { QEventLoop eventLoop; connect(networkAccessManager.get(), &QNetworkAccessManager::finished, &eventLoop, &QEventLoop::quit); int count = 0; while (count < retryCount) { QNetworkReply* reply = networkAccessManager->get(request); eventLoop.exec(); if (reply != NULL && reply->error() == 0) return true; count++; } return false; }
**networkAccessManager is the QNetworkAccessManager object.
-
Hi and welcome to devnet,
In your code, once your call
.get
and then->get()
. How can that compile ?You should also post the stack trace of your crash.
-
The crash is probably from the connect line:
connect(networkAccessManager.get(),... // should be ? connect(networkAccessManager,...)
I don't think any of this would be necessary if you used the signals and slots to handle the events (as opposed to a loop waiting for a response). Just a thought.
-
What scope is this?
{ SharedPtr<QNetworkAccessManager> networkAccessManager; networkAccessManager(new QNetworkAccessManager()); bool res = write(request); }
You have declared networkAccessManager locally in that scope. What networkAccessManager are you referring to inside the Write method?