Results 1 to 6 of 6

Thread: Segmentation fault using sqlite

  1. #1
    Join Date
    Oct 2014
    Location
    Spain
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Segmentation fault using sqlite

    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:

    Qt Code:
    1. void Ligas::SetId(const QString s,QSqlDatabase *datos)
    2. {
    3. if(datos == NULL){
    4. qDebug() << "NULL database";
    5. return;
    6. }
    7. if(!datos->isOpen()){
    8. qDebug() << "Failed to open the database";
    9. return;
    10. }
    11. qDebug() << "step 1: " << s;
    12. setWindowTitle(s);
    13. qDebug() << "step 2";
    14. ui->listWidget_estad->clear();
    15. qDebug() << "step 3";
    16. ui->listWidget_nombres->clear();
    17. qDebug() << "step 4";
    18. ...
    19. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault using sqlite

    Is the Ligas object you are calling this on a valid one?

    Cheers,
    _

  3. #3
    Join Date
    Oct 2014
    Location
    Spain
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Segmentation fault using sqlite

    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:
    Qt Code:
    1. #ifndef LIGAS_H
    2. #define LIGAS_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtSql>
    6.  
    7. namespace Ui {
    8. class Ligas;
    9. }
    10.  
    11. class Ligas : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit Ligas(QWidget *parent = 0);
    17. ~Ligas();
    18.  
    19. public slots:
    20. void SetId(const QString,QSqlDatabase *);
    21.  
    22. private:
    23. Ui::Ligas *ui;
    24. };
    25.  
    26. #endif // LIGAS_H
    To copy to clipboard, switch view to plain text mode 

    And this is ligas.cpp:

    Qt Code:
    1. #include "ligas.h"
    2. #include "mainwindow.h"
    3. #include "ui_ligas.h"
    4.  
    5. Ligas::Ligas(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::Ligas)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. Ligas::~Ligas()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void Ligas::SetId(const QString s,QSqlDatabase *datos)
    18. {...}
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,546
    Thanks
    13
    Thanked 1,578 Times in 1,507 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Segmentation fault using sqlite

    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.

  5. #5
    Join Date
    Oct 2014
    Location
    Spain
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Segmentation fault using sqlite

    Quote Originally Posted by ChrisW67 View Post
    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:

    Qt Code:
    1. void MainWindow::on_listWidget_competiciones_doubleClicked(const QModelIndex &index)
    2. {
    3. qDebug() << "doubleclick";
    4. if(!datos.isOpen()){
    5. qDebug() << "Failed to open the database";
    6. return;
    7. }
    8. ligas->SetId(index.data().toString(),&datos);
    9. ligas->show();
    10. ligas->raise();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by ChrisW67 View Post
    BTW,it is a little unusual to pass a QSqlDatabase by pointer.
    So, what is the most usual way to do that ? To define it as a global variable?
    Last edited by vidalpuga; 3rd October 2014 at 23:16.

  6. #6
    Join Date
    Oct 2014
    Location
    Spain
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Segmentation fault using sqlite [SOLVED]

    Quote Originally Posted by anda_skoa View Post
    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?

Similar Threads

  1. QWT - Segmentation Fault
    By Wojtek.wk in forum Newbie
    Replies: 0
    Last Post: 17th April 2010, 14:29
  2. Segmentation fault
    By Schimanski in forum Qt Programming
    Replies: 20
    Last Post: 31st August 2009, 16:26
  3. segmentation fault
    By uchennaanyanwu in forum Newbie
    Replies: 3
    Last Post: 31st July 2008, 16:52
  4. Segmentation Fault?!
    By r07f1 in forum Newbie
    Replies: 2
    Last Post: 11th April 2008, 15:10
  5. segmentation fault
    By shamik in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2006, 07:33

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.