0
votes

I have been working on a Qt Creator application, and just recently ran into a run-time error that I have no idea how to find and fix. The program just unexpectedly finishes once it starts up, and when I run the debug, it gives me the error:

The inferior stopped because it received a signal from the operating system.

Signal name: SIGSEGV

Signal meaning: Segmentation fault

Now I understand that this error is caused by attempted access to an invalid memory location, but I have absolutely no clue what I did to trigger it. The debugger seems to be pointing me to a specific line of code:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QString>
#include "ui_mainwindow.h"
#include "foamdata.h"
#include "domaingeneration.h"


namespace Ui {
    class MainWindow;
}


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    int getXDim() const { return ui->xDim->value(); } // THIS is where it points to!!
    int getYDim() const { return ui->yDim->value(); }


private slots:
    void on_findDir_clicked();

    void on_create_clicked();

    void on_generateDomain_clicked();

private:
    Ui::MainWindow *ui;
    DomainGeneration dg;    //declaring second window as a member of main window
};

#endif // MAINWINDOW_H

That function is used in this:

#include "domaingeneration.h"
#include "ui_domaingeneration.h"
#include <QCheckBox>
#include "mainwindow.h"

DomainGeneration::DomainGeneration(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DomainGeneration)
{
    ui->setupUi(this);

    // Generating checkboxes for domain generation
    MainWindow* main;
    int x_dim = main->getXDim();
    int y_dim = main->getYDim();
    QVector<QCheckBox*> checkBoxVector;
    for(int i = 0; i < x_dim; ++i){
        for(int j = 0; j < y_dim; ++j){
            checkBoxVector.append(new QCheckBox(this));
            checkBoxVector.last()->setGeometry(i * 20, j * 20, 20, 20);
        }
    }
}


DomainGeneration::~DomainGeneration()
{
    delete ui;
}

Here is the mainwindow source code:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "functions.h"
#include "main.cpp"
#include <QtGui/QApplication>
#include <QFileDialog>
#include <fstream>
#include "foamdata.h"
#include "domaingeneration.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}




// This is the function that handles the directory search when the 'browse' button is pressed
void MainWindow::on_findDir_clicked()
{
    QString path;   //declaring the path to the base directory

    path = QFileDialog::getExistingDirectory(   //gathering the directory from QFileDialog class
        this, tr("Choose the project directory"),
        "/home",
        QFileDialog::ShowDirsOnly
        | QFileDialog::DontResolveSymlinks );

    ui->baseDir->setText( path );   //setting the retrieved path into the line edit box
}



// This function makes the project by creating a new directory with name 'project_name'
// in the base directory, and collects the OpenFOAM version number and simulation type
void MainWindow::on_create_clicked()
{

    QString project_name, foam_version, full_dir, slash, base_dir;
    base_dir = ui->baseDir->text();
    project_name = ui->projectName->text();     //getting the text from the 'projectName' field
    foam_version = ui->version->currentText();  //getting the selection from the 'version' drop-down box

    project_info.save_version(foam_version);        //saving data in a global project variable
    project_info.save_projName(project_name);
    project_info.save_simType(ui->simType->currentText());

    //first checking if the fields have input in them:
    QString blank = "";
    QString no_text0 = "Please enter project name";
    if( project_name==no_text0 || (project_name==blank) ) {
        ui->projectName->setText( no_text0 );
        return;
    }


    QString no_text1 = "Please enter a valid directory";
    if( base_dir==no_text1 || base_dir==blank || !QDir( base_dir ).exists() ) {
        ui->baseDir->setText( no_text1 );
        return;
    }


    slash = "/";        // needed to separate folders in the directory (can't use a literal)
    full_dir = base_dir.append(slash.append(project_name));
    project_info.save_directory(full_dir);

    if( !QDir(full_dir).exists() )  //check if directory already exists
        QDir().mkdir(full_dir);     //creating directory

    QString blockmesh_filename, suffix;
    suffix = "_blockMeshDict";
    slash = "/";    //must re-define
    blockmesh_filename = full_dir.append( slash.append( project_name.append(suffix) ) );
    std::ofstream create_file( blockmesh_filename.toStdString().c_str() );  //creating empty blockmesh file

}


void MainWindow::on_generateDomain_clicked()    //opening the new window for domain generation
{
    dg.show();
}

Can anybody help me find what the heck is going on?

3
Where is the MainWindow constructor? Does it set ui to point anywhere?Bo Persson
I second Bo's request, currently one can't tell much about your code from this code fragment.Khaled Nassar
I added the whole source codeMike N.
I'd say the cause of the error is the one in my answer or Bo's, try passing a pointer of MainWindow to DomainGeneration, and do your stuff with it.Khaled Nassar

3 Answers

4
votes

The obvious is that you're not initializing MainWindow * main;:

MainWindow* main; // right here
int x_dim = main->getXDim();
int y_dim = main->getYDim();

There might, however, be other uninitialized values, the piece of code presented doesn't really say much.

1
votes

Or here:

// Generating checkboxes for domain generation
MainWindow* main;
int x_dim = main->getXDim();
int y_dim = main->getYDim();

main definitely doesn't point anywhere!

0
votes

You have a private data member ui in class MainWindow that is NULL. That variable is never set. In fact, there is no no way to set it. (Where is the setter?)