Application crash on QEventloop::exec() on MacOs
-
@kshegunov This is my implementation:-
QJsonArray Contact_API_Comm::get_pending_Requests(QString token, QString url)
{QUrl serviceUrl = QUrl(url); QNetworkRequest request(serviceUrl); request.setRawHeader("Authorization",token.toUtf8()); Networkmanager->get(request); QObject::connect(Networkmanager, SIGNAL(finished(QNetworkReply*)), this,SLOT(serviceContactRequestFinished(QNetworkReply*))); loop.exec(); disconnect(Networkmanager, SIGNAL(finished(QNetworkReply*)), this,SLOT(serviceContactRequestFinished(QNetworkReply*))); return user_contact;
}
void Contact_API_Comm::serviceContactRequestFinished(QNetworkReply *reply)
{QVariant Status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); QByteArray buffer = reply->readAll(); QJsonDocument jsonDoc(QJsonDocument::fromJson(buffer)); qDebug() << jsonDoc; QJsonObject jsonReply = jsonDoc.object(); if(Status_code.toInt() == 200) { if(jsonReply.contains("userlist")) { user_contact = jsonReply["userlist"].toArray(); } if(jsonReply.contains("message")){ message = jsonReply["message"].toString(); } } else if(Status_code.toInt() == 422) { message = jsonReply["message"].toString(); qDebug()<<message; } else { message = "Could not connect at the moment"; } reply->deleteLater(); loop.exit(0);
}
-
@kshegunov This is my implementation:-
QJsonArray Contact_API_Comm::get_pending_Requests(QString token, QString url)
{QUrl serviceUrl = QUrl(url); QNetworkRequest request(serviceUrl); request.setRawHeader("Authorization",token.toUtf8()); Networkmanager->get(request); QObject::connect(Networkmanager, SIGNAL(finished(QNetworkReply*)), this,SLOT(serviceContactRequestFinished(QNetworkReply*))); loop.exec(); disconnect(Networkmanager, SIGNAL(finished(QNetworkReply*)), this,SLOT(serviceContactRequestFinished(QNetworkReply*))); return user_contact;
}
void Contact_API_Comm::serviceContactRequestFinished(QNetworkReply *reply)
{QVariant Status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); QByteArray buffer = reply->readAll(); QJsonDocument jsonDoc(QJsonDocument::fromJson(buffer)); qDebug() << jsonDoc; QJsonObject jsonReply = jsonDoc.object(); if(Status_code.toInt() == 200) { if(jsonReply.contains("userlist")) { user_contact = jsonReply["userlist"].toArray(); } if(jsonReply.contains("message")){ message = jsonReply["message"].toString(); } } else if(Status_code.toInt() == 422) { message = jsonReply["message"].toString(); qDebug()<<message; } else { message = "Could not connect at the moment"; } reply->deleteLater(); loop.exit(0);
}
Don't use local event loops, especially where when there's no pressing need to. You're spinning the global even loop and you have no idea what events are processed in the meantime. Here's a suggestion:
QJsonArray Contact_API_Comm::get_pending_Requests(QString token, QString url) { QNetworkRequest request(QUrl(url)); request.setRawHeader("Authorization", token.toUtf8()); QNetworkReply * reply = Networkmanager->get(request); QObject::connect(reply, &QNetworkReply::finished, this, std::bind(&Contact_API_Comm::serviceContactRequestFinished, this, reply)); QObject::connect(reply, &QNetworkReply::finished, reply, &QObject::deleteLater); } void Contact_API_Comm::serviceContactRequestFinished(QNetworkReply *reply) { QJsonArray result; // Read the json data into `result` emit contactReceived(result); //< This signal you declare yourself and connect something to it to process the data further }
-
@kshegunov I have tried this but now the app is crashing with this stack trace
1 objc_msgSend (x86_64h) /usr/lib/libobjc.A.dylib 0x7fff6051b6a9
2 (anonymous namespace)::AutoreleasePoolPage::pop(void *) (x86_64h) /usr/lib/libobjc.A.dylib 0x7fff6051e47c
3 _CFAutoreleasePoolPop (x86_64h) /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation 0x7fff35d4fd4a
4 -[NSAutoreleasePool drain] (x86_64) /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation 0x7fff37ffa792
5 -[NSApplication run] (x86_64) /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit 0x7fff3336c5f7
6 QCocoaEventDispatcher::processEvents(QFlagsQEventLoop::ProcessEventsFlag) (x86_64) /Users/x/Qt/5.12.6/clang_64/plugins/platforms/libqcocoa.dylib 0x1029fd013
7 QEventLoop::exec(QFlagsQEventLoop::ProcessEventsFlag) (x86_64) /Users/x/Qt/5.12.6/clang_64/lib/QtCore.framework/Versions/5/QtCore 0x1018b1b8f
8 QCoreApplication::exec() (x86_64) /Users/x/Qt/5.12.6/clang_64/lib/QtCore.framework/Versions/5/QtCore 0x1018b6b82
9 main (x86_64) /Users/x/build-Aact-Desktop_Qt_5_12_6_clang_64bit-Release/Aact.app/Contents/MacOS/Aact 0x100079e7b
10 start (x86_64) /usr/lib/system/libdyld.dylib 0x7fff61cf73d5 -
Hi,
What version of macOS are you running ?
-
You seem to have a lot of QEventLoop instances spread over your code, why so many ?
-
@Anj_San said in Application crash on QEventloop::exec() on MacOs:
Is using too many even loop instance is a wrong?
Yes. Use assynchronous nature of Qt like @kshegunov shown
-
@jsulm I have tried @kshegunov 's way but the code became more complicated and me confused.
Is there no way to save it while using qEventLoop::exec()?