App crashes when closed
-
Can someone explain to me why this app runs fine but crashes when i close it, is it because i ran it on a WIN 7 PC?
#include "widget.h" #include "ui_widget.h" #include <QJsonDocument> #include <QJsonArray> #include <QJsonObject> #include <QJsonValue> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) ,netManager(new QNetworkAccessManager(this)) ,netReply(nullptr) ,dataBuffer(new QByteArray) { ui->setupUi(this); pres = new QString("Here's a List of our enemies and their co-ordinates Mr.President!!"); timer = new QTimer(this); timer->setInterval(20); connect(timer,SIGNAL(timeout()),this,SLOT(updateText())); } Widget::~Widget() { delete ui; } void Widget::on_fetchButton_clicked() { QNetworkRequest netReq{QUrl("https://jsonplaceholder.typicode.com/users")}; netReply = netManager->get(netReq); timer->start(); connect(netReply,&QNetworkReply::readyRead,this,&Widget::readData); connect(netReply,&QNetworkReply::finished,this,&Widget::finishRead); } void Widget::readData() { dataBuffer->append(netReply->readAll()); } void Widget::finishRead() { if(netReply->error()) qDebug() << "Error - " << netReply->errorString(); else{ //CONVERT THE RECEIVED DATA FROM A QBYTEARRAY TO A JSON DOC auto doc = QJsonDocument::fromJson(*dataBuffer); //BREAK DOWN THE DOCUMENT INTO JSON-ARRAYS AND OBJECTS FOR DATA RETRIEVAL auto jsonArray = doc.array(); for(int i{0}; i < jsonArray.size(); ++i){ auto jObj = jsonArray.at(i).toObject(); auto name = jObj.value("name").toString(); QJsonObject address = jObj.value("address").toObject().value("geo").toObject(); auto lat = address.value("lat").toString(); auto lng = address.value("lng").toString(); qDebug() << name << " " << lat << " " << lng; ui->listWidget->addItem(QString(name + " (%1,%2)").arg(lat,lng)); } } } void Widget::updateText() { if(ui->lineEdit->text().length() == pres->length()){ qDebug() << "Stopped!"; timer->stop(); return; } if(ui->lineEdit->text().isEmpty()){ ui->lineEdit->setText("H"); return; } auto txt = ui->lineEdit->text(); txt.append(pres->at(i)); ui->lineEdit->setText(txt); ++i; }
-
Header
#pragma once #include <QWidget> #include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> #include <QByteArray> #include <QDebug> #include <QTimer> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); private slots: void on_fetchButton_clicked(); void readData(); void finishRead(); void updateText(); private: Ui::Widget *ui; QNetworkAccessManager *netManager; QNetworkReply *netReply; QByteArray * dataBuffer; QTimer *timer; QString *pres; int i{1}; };
-
Use a debugger, see where it crashes and fix it.
-
-
Hi,
@Padawan said in App crashes when closed:
QByteArray * dataBuffer;
QString *pres;Neither of these needs to be stored on the heap.
@Padawan said in App crashes when closed:
int i{1};
You might want to reserve that for the for loops. It's a bit too generic and your use of it in the code is not really clear.
-
@Padawan
The pane you are interested in is not the big one but the one in the bottom-left corner. That shows the stack trace, where the code was executing at the moment of the crash. Although memory mess-ups like this could emanate from elsewhere and be spurious, it looks like the issue lies in SSL. Somehow related to yourget
request tohttps
. That's where I would look, nothing to do with the JSON or UI. Is your version of OpenSSL for Qt good?