QNetworkAccessManager finished signal error
-
Hi,
Any chance of having something like:
void MyCoolClass::getUpate() { Updater updater; updater.getToken(); }
?
-
@SGaist @mcosta I think I got a clue, In
void MyCoolClass::getUpate() { Updater updater; updater.getToken(); this->runUpdate(); }
if I run both then only 'this->runUpdate();' call will work if commented out 'this->runUpdate();' then 'updater.getToken();' works.
void MyCoolClass::runUpdate() { QNetworkAccessManager *manager = new QNetworkAccessManager(this); request.setUrl(QUrl("http://localhost/cdms/pro/index.php/update/linux?version=4.14&" "lisenceRegis=1&onlineRegis=1")); QNetworkReply *reply = manager->get(request); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); } void CDMS::replyFinished(QNetworkReply * replay) { // Checking replay if (replay->isReadable()) { // Checking errors if(replay->error() == QNetworkReply::NoError) { QByteArray strreplay = replay->readAll(); // update ststus check if(strreplay != "0") { } else { ui->statusBar->showMessage("No new updates found", 10000); // run update on intervel in 30Minute QTimer::singleShot(1800000, this, SLOT(updateOnlineTimer())); // Run update on data and settings //Updater updateCDMSdata; //updateCDMSdata.getToken(); } } else { ui->statusBar->showMessage(replay->errorString(), 10000); // run update on intervel in 1Minute QTimer::singleShot(60000, this, SLOT(updateOnlineTimer())); } } }
I want to call the 'Updater' when the 'runUpdate' download finished.
-
You create a local instance of Updater in this method:
void MyCoolClass::getUpate() { Updater updater; updater.getToken(); this->runUpdate(); }
As soon as this method finishes updater goes out of scope and is destroyed. That's why nothing is called. You must keep the instance until it finishes its work.
-
But now you have a memory leak: you do not delete updateCDMSdata
-
To add to @jsulm, what does runUpdate do ? Call a function on updateCDMSdata ? If so you are lucky id doesn't crash.
If you will be using Updater several times then make it a member of your MyCoolClass object and don't forget to handle its deletion.
-
@SGaist , runUpdate used to update the software and updateCDMSdata used to update data. I want to run data update when there is no software update, first check software update then from the 'finished(QNetworkReply*)' signal data update started.
I need to call updater several times, so I think use Updater as a member and delete on MyCoolClass::~MyCoolClass() is a better idea ?
-
Since it's a QObject you can use the parent/child paradigm to let Qt handle the deletion for you.