Program crashes (without error message) when attempting to read a second file into my program
-
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)); }
-
@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.
-
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.
-
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.
@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?
-
@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?
@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.
-
@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?
@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 otherinvalid value that you're not checking for prior to attempting to use. -
@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 otherinvalid value that you're not checking for prior to attempting to use.@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.
-
@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.
@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.
-
@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.
-
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.
-
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.
-
@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.
@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 unlessentryNum
is always 0. I don't know what you think you are doing with localQStringList fileName
/fileData
and then trying to insert into them at a fixedentryNum
, the lists are empty but meanwhile I suspect you are trying to insert into the middle.... -
@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 unlessentryNum
is always 0. I don't know what you think you are doing with localQStringList fileName
/fileData
and then trying to insert into them at a fixedentryNum
, the lists are empty but meanwhile I suspect you are trying to insert into the middle....@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?
-
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
-
@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?
@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
QStringList
s 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.
-
@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.
-
@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.
@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. -
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.
@Christian-Ehrlicher sorry, Could you explain what debuger in this contect? Im newbie here and I have same problem
-
@Christian-Ehrlicher sorry, Could you explain what debuger in this contect? Im newbie here and I have same problem
@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.