Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. App crashes when closed
Qt 6.11 is out! See what's new in the release blog

App crashes when closed

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 706 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • PadawanP Offline
    PadawanP Offline
    Padawan
    wrote on last edited by
    #1

    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;
    }
    

    Ningen

    1 Reply Last reply
    0
    • PadawanP Offline
      PadawanP Offline
      Padawan
      wrote on last edited by
      #2

      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};
      };
      

      Ningen

      SGaistS 1 Reply Last reply
      0
      • JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        Run it in a debugger and see if you get a stack trace when it crashes?
        Reduce your code to something minimal, e.g. is all of that JSON code of the slightest relevance, do you need line edits (or indeed any UI)?

        PadawanP 1 Reply Last reply
        0
        • JonBJ JonB

          Run it in a debugger and see if you get a stack trace when it crashes?
          Reduce your code to something minimal, e.g. is all of that JSON code of the slightest relevance, do you need line edits (or indeed any UI)?

          PadawanP Offline
          PadawanP Offline
          Padawan
          wrote on last edited by
          #4

          @JonB it was there before the lineEdit, and the JSON code is the main thing, i'm just learning how to parse a JSON file and get data from it...i will do the stack trace stuff

          Ningen

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Use a debugger, see where it crashes and fix it.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            PadawanP 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              Use a debugger, see where it crashes and fix it.

              PadawanP Offline
              PadawanP Offline
              Padawan
              wrote on last edited by
              #6

              @Christian-Ehrlicher 6e6f9938-474b-42f7-8d4f-d36d3357399c-image.png

              Ningen

              JonBJ 1 Reply Last reply
              0
              • PadawanP Offline
                PadawanP Offline
                Padawan
                wrote on last edited by Padawan
                #7

                doesnt look like it crashes in the code itself, and i dont even understand what that stuff is

                Ningen

                1 Reply Last reply
                0
                • PadawanP Padawan

                  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};
                  };
                  
                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • PadawanP Padawan

                    @Christian-Ehrlicher 6e6f9938-474b-42f7-8d4f-d36d3357399c-image.png

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @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 your get request to https. That's where I would look, nothing to do with the JSON or UI. Is your version of OpenSSL for Qt good?

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved