I'm building a Qt app and it's crashing because of a segmentation fault. After investigation, I found out that the cause of the segfault is that "this" is NULL and I try to access a member variable in the readInputFile(QString path) method.
In this line input += line;
I don't understand why this is happening. how can "this" be NULL ?
Here's where the object is created
void MainWindow::on_inpFileCheck_clicked()
{
if (ui->inpFileCheck->isChecked()) {
QString filePath = QFileDialog::getOpenFileName(this,tr("Open CSV file"), "/home", tr("CSV (*.csv)"));
myAlgo->readInputFile(filePath);
ui->inputEdit->clear();
ui->inputEdit->appendPlainText(myAlgo->getInput());
}
}
Here's the BaseAlgorithm header
#include "qstring.h"
#include "qmainwindow.h"
class BaseAlgorithm
{
public:
BaseAlgorithm();
QString readInputFile(QString);
int lenArr;
private:
QString input;
QString output;
};
And here's the implementation and where the problem happens
#include "basealgorithm.h"
#include "qfile.h"
#include "qtextstream.h"
BaseAlgorithm::BaseAlgorithm() {
numComparisons = 0;
input = "";
output = "";
intArr = NULL;
}
QString BaseAlgorithm::readInputFile(QString path) {
QFile inpFile(path);
if (inpFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&inpFile);
while (!in.atEnd()) {
QString line = in.readLine();
input += line; // crash happens here
}
return input;
}
else {
return "ERROR";
}
}