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. Program crashes (without error message) when attempting to read a second file into my program
Forum Updated to NodeBB v4.3 + New Features

Program crashes (without error message) when attempting to read a second file into my program

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 7 Posters 1.5k Views 2 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.
  • S Offline
    S Offline
    SOrton
    wrote on 2 Jan 2023, 16:23 last edited by SOrton 1 Feb 2023, 16:32
    #1

    Hi,
    as title says I'm attempting to read a second file into my program using QFileDialog. I successfully read and store infomation from one file, and then using QStringList i want to add more data into a variable. I'm assuming I'm allocating memory incorrectly but hoping for an explanation.

    Code:

    void MainWindow::on_btnAddData_clicked()
    {//Button loads new dialog to add data
        //objAddDataDialog = new AddDataDialog(this); //makes button a child of main dialog
        //objAddDataDialog->show();
        QString filePath=QFileDialog::getOpenFileName(this, tr("Find CSV file"), "C://", "All files (*.*);;CSV File (*.csv)");
        //Above saves the file path of the file to be read.
    
        readData(filePath);
    
    }
    void MainWindow::readData(QString filePath)
    {
        int entryNum = ui->comboData->count();
        qInfo() << entryNum;
        QStringList fileName;
        QStringList fileData;
    
        QFile file(filePath);
        if (!file.exists())
        {
            qCritical() << "File Not Found.";
            return;
        }
        if (!file.open(QIODevice::ReadOnly))
        {
            qCritical() << "Could not open file: ";
            qCritical() << file.errorString();
            return;
        }
    
        QFileInfo fi(file);
        //fileName = fi.fileName();
        fileName.insert(entryNum, fi.fileName());
        qInfo() << fileName.at(entryNum);
    
        QTextStream stream(&file);
        //while(!stream.atEnd())
        //{
           QString line = stream.readLine();
           fileData.insert(entryNum, line);
        //}
    
    
        file.close();
    
        qInfo() << fileData.at(entryNum);
        ui->comboData->addItem(fileName.at(entryNum));
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SOrton
      wrote on 4 Jan 2023, 13:03 last edited by
      #16

      @JonB I've managed to fix it, thanks for all the help - I was doing the stack view I wasn't just ignoring you I was trying to figure it out myself before asking!

      When passing my strings into the function for some (I don't understand) a * pointer to the variables did not work but a & does. Sorry this wasn't a Qt issue - completely my fault.

      J 1 Reply Last reply 4 Jan 2023, 13:11
      0
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 2 Jan 2023, 16:29 last edited by
        #2

        As always when a program crashes - use a debugger and find out where exactly it crashes and why.

        And please properly format your code with the code tags so others can read it.

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

        S P 2 Replies Last reply 2 Jan 2023, 16:39
        0
        • C Christian Ehrlicher
          2 Jan 2023, 16:29

          As always when a program crashes - use a debugger and find out where exactly it crashes and why.

          And please properly format your code with the code tags so others can read it.

          S Offline
          S Offline
          SOrton
          wrote on 2 Jan 2023, 16:39 last edited by
          #3

          @Christian-Ehrlicher
          Hi, thanks for the quick reply! I've fixed the code issue.

          Debugger gives me SIGSEVG, Segmentation fault. From what I understand this is to due with memory as I assumed. From my understanding QStringList is a dynamic structure, and therefore I'm unsure as to why I'm reaching this error?

          C M 2 Replies Last reply 2 Jan 2023, 16:42
          0
          • S SOrton
            2 Jan 2023, 16:39

            @Christian-Ehrlicher
            Hi, thanks for the quick reply! I've fixed the code issue.

            Debugger gives me SIGSEVG, Segmentation fault. From what I understand this is to due with memory as I assumed. From my understanding QStringList is a dynamic structure, and therefore I'm unsure as to why I'm reaching this error?

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 2 Jan 2023, 16:42 last edited by
            #4

            @SOrton said in Program crashes (without error message) when attempting to read a second file into my program:

            Debugger gives me SIGSEVG, Segmentation fault.

            And from where does the crash comes from? Look at the backtrace to see what piece of code from you triggers the crash 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

            J 1 Reply Last reply 4 Jan 2023, 11:56
            1
            • S SOrton
              2 Jan 2023, 16:39

              @Christian-Ehrlicher
              Hi, thanks for the quick reply! I've fixed the code issue.

              Debugger gives me SIGSEVG, Segmentation fault. From what I understand this is to due with memory as I assumed. From my understanding QStringList is a dynamic structure, and therefore I'm unsure as to why I'm reaching this error?

              M Offline
              M Offline
              mzimmers
              wrote on 2 Jan 2023, 18:31 last edited by mzimmers 1 Feb 2023, 18:44
              #5

              @SOrton I'm going to guess that this line:

              QString filePath=QFileDialog::getOpenFileName(this, tr("Find CSV file"), "C://", "All files (*.*);;CSV File (*.csv)");
              

              Is returning a[n] nullptr or some other invalid value that you're not checking for prior to attempting to use.

              C 1 Reply Last reply 2 Jan 2023, 18:40
              0
              • M mzimmers
                2 Jan 2023, 18:31

                @SOrton I'm going to guess that this line:

                QString filePath=QFileDialog::getOpenFileName(this, tr("Find CSV file"), "C://", "All files (*.*);;CSV File (*.csv)");
                

                Is returning a[n] nullptr or some other invalid value that you're not checking for prior to attempting to use.

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 2 Jan 2023, 18:40 last edited by Christian Ehrlicher 1 Feb 2023, 18:40
                #6

                @mzimmers said in Program crashes (without error message) when attempting to read a second file into my program:

                Is returning a nullptr

                Since this function does not return a pointer you won't get a nullptr here either.

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

                M 1 Reply Last reply 2 Jan 2023, 18:48
                1
                • C Christian Ehrlicher
                  2 Jan 2023, 18:40

                  @mzimmers said in Program crashes (without error message) when attempting to read a second file into my program:

                  Is returning a nullptr

                  Since this function does not return a pointer you won't get a nullptr here either.

                  M Offline
                  M Offline
                  mzimmers
                  wrote on 2 Jan 2023, 18:48 last edited by
                  #7

                  @Christian-Ehrlicher thanks for pointing that out. I was trying to suggest that the returned value needs to be checked for some validity before being used, but I could be mistaken.

                  J 1 Reply Last reply 2 Jan 2023, 18:55
                  0
                  • M mzimmers
                    2 Jan 2023, 18:48

                    @Christian-Ehrlicher thanks for pointing that out. I was trying to suggest that the returned value needs to be checked for some validity before being used, but I could be mistaken.

                    J Offline
                    J Offline
                    JonB
                    wrote on 2 Jan 2023, 18:55 last edited by
                    #8

                    @mzimmers
                    At worst an empty (but not nullptr) QString gets passed to QFile to check/open. I don't think that will cause the OP's SEGV.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 2 Jan 2023, 18:57 last edited by
                      #9

                      Hi,

                      From a cursory look, I would suggest to ensure that entryNum contains a valid value with regard to all the accesses you are doing using it.

                      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
                      0
                      • S Offline
                        S Offline
                        SOrton
                        wrote on 4 Jan 2023, 11:17 last edited by
                        #10

                        Hi all.

                        The error is occuring on

                        fileName.insert(entryNum, fi.fileName());
                        

                        BUT only on my second run through - it works perfectly for the first file. I changed insert for append and get the same result so I don't think entryNum is the issue.

                        I can seccesfully use:

                        qDebug() << fi.fileName() << "1";
                        

                        and it prints the file correct file name that I'm attempting to store so its definitely a issue with QStringList I just cant figure out what.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SOrton
                          wrote on 4 Jan 2023, 11:31 last edited by
                          #11

                          In addition if I comment out the fileName lines, the program successfully stores all file data. Could my issue be the QFileInfo line somhow?

                          1 Reply Last reply
                          0
                          • C Christian Ehrlicher
                            2 Jan 2023, 16:42

                            @SOrton said in Program crashes (without error message) when attempting to read a second file into my program:

                            Debugger gives me SIGSEVG, Segmentation fault.

                            And from where does the crash comes from? Look at the backtrace to see what piece of code from you triggers the crash and fix it.

                            J Offline
                            J Offline
                            JonB
                            wrote on 4 Jan 2023, 11:56 last edited by JonB 1 Apr 2023, 12:01
                            #12

                            @SOrton

                            @Christian-Ehrlicher said in Program crashes (without error message) when attempting to read a second file into my program:

                            Look at the backtrace to see what piece of code from you triggers the crash and fix it.

                            If you are getting a SEGV please run it from debugger and (a) show the complete stack trace when it occurs and (b) have a look round at the variables' values etc.

                            I don't see that your readData() makes much sense unless entryNum is always 0. I don't know what you think you are doing with local QStringList fileName/fileData and then trying to insert into them at a fixed entryNum, the lists are empty but meanwhile I suspect you are trying to insert into the middle....

                            S 1 Reply Last reply 4 Jan 2023, 12:15
                            0
                            • J JonB
                              4 Jan 2023, 11:56

                              @SOrton

                              @Christian-Ehrlicher said in Program crashes (without error message) when attempting to read a second file into my program:

                              Look at the backtrace to see what piece of code from you triggers the crash and fix it.

                              If you are getting a SEGV please run it from debugger and (a) show the complete stack trace when it occurs and (b) have a look round at the variables' values etc.

                              I don't see that your readData() makes much sense unless entryNum is always 0. I don't know what you think you are doing with local QStringList fileName/fileData and then trying to insert into them at a fixed entryNum, the lists are empty but meanwhile I suspect you are trying to insert into the middle....

                              S Offline
                              S Offline
                              SOrton
                              wrote on 4 Jan 2023, 12:15 last edited by
                              #13

                              @JonB I have swapped insert for append and receive the same issue (shouldn't be in the middle this way). In addition the error only seems to affect fileName and not fileData which confuses me as i'm effectively doing the same thing for them both.

                              in debug I get this error

                              ASSERT failure in QList::at: "index out of range", file C:/Qt/6.3.2/mingw_64/include/QtCore/qlist.h, line 459
                              

                              But again, from my understanding append adds a value to end of list and increases its size by 1; so how can this happen?

                              J 1 Reply Last reply 4 Jan 2023, 12:47
                              0
                              • S Offline
                                S Offline
                                SOrton
                                wrote on 4 Jan 2023, 12:30 last edited by
                                #14

                                I'm gunna repost my code cos I think i've reordered it in a better way:
                                The lists are now declared in my header file.

                                void MainWindow::on_btnAddData_clicked()
                                {//Button loads new dialog to add data
                                    //objAddDataDialog = new AddDataDialog(this); //makes button a child of main dialog
                                    //objAddDataDialog->show();
                                    QString filePath=QFileDialog::getOpenFileName(this, tr("Find CSV file"), "C://", "All files (*.*);;CSV File (*.csv)");
                                    //Above saves the file path of the file to be read.
                                
                                    readData(filePath, fileName, fileData);
                                
                                }
                                
                                void MainWindow::readData(QString filePath, QStringList fileName, QStringList fileData)
                                {
                                    int entryNum = ui->comboData->count();
                                    qInfo() << entryNum << " entry";
                                
                                
                                    QFile file(filePath);
                                
                                    if (!file.exists())
                                    {
                                        qCritical() << "File Not Found.";
                                        return;
                                    }
                                
                                    if (!file.open(QIODevice::ReadOnly))
                                    {
                                        qCritical() << "Could not open file: ";
                                        qCritical() << file.errorString();
                                        return;
                                    }
                                
                                    QFileInfo fi(file);
                                    //fileName = fi.fileName();
                                
                                    qDebug() << fi.fileName() << "1";
                                    QString temp = fi.fileName();
                                    fileName.append(temp); //ERROR OCCURS HERE
                                    qDebug() << temp << temp;
                                
                                    qInfo() << "teas";
                                    //qInfo() << fileName.at(entryNum);
                                
                                    QTextStream stream(&file);
                                    //while(!stream.atEnd())
                                    //{
                                       QString line = stream.readAll();
                                       fileData.append(line);
                                    //}
                                
                                
                                    file.flush();
                                    file.close();
                                    fi.refresh();
                                
                                
                                    qInfo() << fileData.at(entryNum);
                                    ui->comboData->addItem(fileName.at(entryNum));
                                    qInfo() << ui->comboData->currentIndex();
                                }
                                void MainWindow::updateWidgets()
                                {
                                   // ui->comboData->addItem(objAddDataDialog->name);
                                
                                }
                                

                                The crash occurs when i try to append the file name into the file list. The file name is read correctly. I Use the exact same method further down and it successfully stores the file data leading to my confusion

                                1 Reply Last reply
                                0
                                • S SOrton
                                  4 Jan 2023, 12:15

                                  @JonB I have swapped insert for append and receive the same issue (shouldn't be in the middle this way). In addition the error only seems to affect fileName and not fileData which confuses me as i'm effectively doing the same thing for them both.

                                  in debug I get this error

                                  ASSERT failure in QList::at: "index out of range", file C:/Qt/6.3.2/mingw_64/include/QtCore/qlist.h, line 459
                                  

                                  But again, from my understanding append adds a value to end of list and increases its size by 1; so how can this happen?

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 4 Jan 2023, 12:47 last edited by JonB 1 Apr 2023, 13:09
                                  #15

                                  @SOrton said in Program crashes (without error message) when attempting to read a second file into my program:

                                  in debug I get this error

                                  One more time: you need to find the stack view pane in the debugger and post it so we can see the backtrace.

                                  void MainWindow::readData(QString filePath, QStringList fileName, QStringList fileData)

                                  These are all copies of the QStringLists passed in. I somehow doubt you intend that?

                                  qInfo() << fileData.at(entryNum);
                                  ui->comboData->addItem(fileName.at(entryNum));

                                  How do you know either of these has at least entryNum elements?

                                  Sorry, I don't understand your code or what you are trying to do. maybe someone else does.

                                  Meanwhile, that does not affect the fact that you should discover the reason for the SEGV, showing the stack trace as mentioned.

                                  1 Reply Last reply
                                  2
                                  • S Offline
                                    S Offline
                                    SOrton
                                    wrote on 4 Jan 2023, 13:03 last edited by
                                    #16

                                    @JonB I've managed to fix it, thanks for all the help - I was doing the stack view I wasn't just ignoring you I was trying to figure it out myself before asking!

                                    When passing my strings into the function for some (I don't understand) a * pointer to the variables did not work but a & does. Sorry this wasn't a Qt issue - completely my fault.

                                    J 1 Reply Last reply 4 Jan 2023, 13:11
                                    0
                                    • S SOrton
                                      4 Jan 2023, 13:03

                                      @JonB I've managed to fix it, thanks for all the help - I was doing the stack view I wasn't just ignoring you I was trying to figure it out myself before asking!

                                      When passing my strings into the function for some (I don't understand) a * pointer to the variables did not work but a & does. Sorry this wasn't a Qt issue - completely my fault.

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 4 Jan 2023, 13:11 last edited by JonB 1 Apr 2023, 13:12
                                      #17

                                      @SOrton
                                      It's difficult to be general, but these days we tend to pass parameters to be changed as &variable references (at the receiving function) rather than pointers. One thing this avoids is the danger of "bad pointers" resulting in "crashes" (like SEGV) which should not happen when using references, they are safer.

                                      1 Reply Last reply
                                      1
                                      • C Christian Ehrlicher
                                        2 Jan 2023, 16:29

                                        As always when a program crashes - use a debugger and find out where exactly it crashes and why.

                                        And please properly format your code with the code tags so others can read it.

                                        P Offline
                                        P Offline
                                        Psyger00
                                        wrote on 15 May 2024, 07:30 last edited by
                                        #18

                                        @Christian-Ehrlicher sorry, Could you explain what debuger in this contect? Im newbie here and I have same problem

                                        jsulmJ 1 Reply Last reply 15 May 2024, 08:16
                                        0
                                        • P Psyger00
                                          15 May 2024, 07:30

                                          @Christian-Ehrlicher sorry, Could you explain what debuger in this contect? Im newbie here and I have same problem

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on 15 May 2024, 08:16 last edited by
                                          #19

                                          @Psyger00 said in Program crashes (without error message) when attempting to read a second file into my program:

                                          Could you explain what debuger in this contect?

                                          Debugger to debug applications. Like GDB on Linux. If you're using QtCreator it already integrates the debugger, just build your app in debug mode and run with debugger.

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          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