PDA

View Full Version : Segmentation fault using sqlite



vidalpuga
3rd October 2014, 18:01
I know this is a typical error message, but I cannot find a solution in the other post I found on it.
I have the following code:


void Ligas::SetId(const QString s,QSqlDatabase *datos)
{
if(datos == NULL){
qDebug() << "NULL database";
return;
}
if(!datos->isOpen()){
qDebug() << "Failed to open the database";
return;
}
qDebug() << "step 1: " << s;
setWindowTitle(s);
qDebug() << "step 2";
ui->listWidget_estad->clear();
qDebug() << "step 3";
ui->listWidget_nombres->clear();
qDebug() << "step 4";
...
}

I get to "Step 1" before the message error:


The inferior stopped because it received a signal from the Operating System.

Signal name : SIGSEGV
Signal meaning : Segmentation fault


If I delete setWindowTitle(s); I get the very same error after Step 2. So I am really confused. Can anybody help me?

The program used to work fine before adding new rows into the database.

anda_skoa
3rd October 2014, 18:16
Is the Ligas object you are calling this on a valid one?

Cheers,
_

vidalpuga
3rd October 2014, 18:59
I think so, because the program worked well before (adding new rows in the database).

The Ligas object is a MainWindow. This is ligas.h:

#ifndef LIGAS_H
#define LIGAS_H

#include <QMainWindow>
#include <QtSql>

namespace Ui {
class Ligas;
}

class Ligas : public QMainWindow
{
Q_OBJECT

public:
explicit Ligas(QWidget *parent = 0);
~Ligas();

public slots:
void SetId(const QString,QSqlDatabase *);

private:
Ui::Ligas *ui;
};

#endif // LIGAS_H

And this is ligas.cpp:


#include "ligas.h"
#include "mainwindow.h"
#include "ui_ligas.h"

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

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

void Ligas::SetId(const QString s,QSqlDatabase *datos)
{...}

ChrisW67
3rd October 2014, 20:25
Your parameter datos can be both not NULL and not valid. Are you sure you are passing a valid pointer that is still valid when this code uses it?

BTW,it is a little unusual to pass a QSqlDatabase by pointer.

vidalpuga
3rd October 2014, 22:27
Your parameter datos can be both not NULL and not valid. Are you sure you are passing a valid pointer that is still valid when this code uses it?I suspect it may be possible that I am passing an invalid pointer. But in that case I do not know why, nor why it used to work fine before.
This is the calling procedure:



void MainWindow::on_listWidget_competiciones_doubleClic ked(const QModelIndex &index)
{
qDebug() << "doubleclick";
if(!datos.isOpen()){
qDebug() << "Failed to open the database";
return;
}
ligas->SetId(index.data().toString(),&datos);
ligas->show();
ligas->raise();
}



BTW,it is a little unusual to pass a QSqlDatabase by pointer.So, what is the most usual way to do that :confused: ? To define it as a global variable?

vidalpuga
4th October 2014, 13:10
Is the Ligas object you are calling this on a valid one?

Cheers,
_OK. I found the mistake. I had deleted "ligas = new Ligas" unwillingly. So that was the problem. Ligas was NULL.

I still don't know what is the best way to pass a QSqlDatabase. Isn't a good ideo to do it by pointer?