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.9k 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.
  • mzimmersM mzimmers

    @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.

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on 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 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 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 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
          • Christian EhrlicherC Christian Ehrlicher

            @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.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #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
            0
            • JonBJ JonB

              @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 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?

              JonBJ 1 Reply Last reply
              0
              • S Offline
                S Offline
                SOrton
                wrote on 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

                  @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?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #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 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.

                    JonBJ 1 Reply Last reply
                    0
                    • S SOrton

                      @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.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #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
                      • Christian EhrlicherC Christian Ehrlicher

                        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 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
                        0
                        • P Psyger00

                          @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 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