Dereferencing not NULL pointer crashes

  • Hello

    I have a QDialog with a public member function "toFront".
    QDialog has a private member attribute m_db of type (QsqLDatabase *).

    @qDebug() << "front " << m_db << " " << *m_db;

    in the constructor works fine. Output is:
    @form 0x7fff5fbffa80
    form QSqlDatabase(driver=""QSQLITE"", database=""/Users/christoph/qt_projects/teiledb/teileDatenbank_playground.sqlite"", host="""", port=-1, user="""", open=true)@

    Doing the same in the member function "toFront" leads to a crash when dereferencing the pointer. The adress is the same like above (first line).
    @qDebug() << "form " << m_db ; // ok
    qDebug() << "form " << *m_db ; // crash

    Crash means programm is not responding, no error whatsoever visible.

    QDialog (instance m_formAddCatLoc) itself is a member of MainWindow. QDialogs member function "toFront" is called from MainWindow
    @void MainWindow::addCatReleased(){

    addCatReleased is a SLOT called when a button goes up
    @connect( ui->pb_addCat, SIGNAL(released()), this, SLOT(addCatReleased()));

    Am I missing a basic qt/thread/scope problem?? Looks like a memory protection for me ... but why?


  • How do you allocate m_db?
    Could you show a constructor and toFront source code?

  • [quote author="andreyc" date="1418996164"]How do you allocate m_db?[/quote]
    Ohhhh .... I found my problem.
    QSqlDatabase is a local vaiable of the constructor so it's allocated on the stack and deleted after constructor is done.

    It did not come to me since in every other case I access the Database throug a QSqlTableModel, however that's a copy and not a reference to my database.

    Thanks anyway :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.