"Unable to free statement: connection pointer is NULL" - This warning appears when a pointer is deleted.



  • I get this warning on application output window when I try to set a parent to a pointer:
    "Unable to free statement: connection pointer is NULL".

    @ QSqlQueryModel *statusListModel = new QSqlQueryModel(this);@

    But if I don't set the parent, the warning does not appear.

    @ QSqlQueryModel *statusListModel = new QSqlQueryModel;@

    Here is the whole method:

    @void UserWindow::populateStatusComboBox()
    {
    User userInfo;
    //QSqlQueryModel *statusListModel = new QSqlQueryModel;
    QSqlQueryModel *statusListModel = new QSqlQueryModel(this);

    statusListModel->setQuery(userInfo.getStatusList("user"));
    ui->inputUserStatusComboBox->setModel(statusListModel);
    ui->searchUserStatusComboBox->setModel(statusListModel);
    

    }@

    I call this method in my window constructor:

    @UserWindow::UserWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserWindow)
    {
    ui->setupUi(this);
    this->setWindowTitle("User Account");
    //some code for other widgets
    populateStatusComboBox();
    //more code for other widgets here
    @

    Here is the method that it calls to retrieve records from a database:
    @QSqlQuery User::getStatusList(QString statusType)
    {
    DatabaseControl userDatabaseControl;
    QSqlDatabase pacioliDatabase;
    pacioliDatabase = userDatabaseControl.connectToDatabase();
    QMessageBox msgBox;
    QSqlQuery pacioliQuery(pacioliDatabase);

    if (userDatabaseControl.checkDatabaseConnection() == true)
    {
        pacioliQuery.prepare("SELECT pacioli.fget_status_list(?)");
        pacioliQuery.bindValue(0, statusType);
        pacioliQuery.exec();
    }
    else
    {
        msgBox.setIcon(QMessageBox::Critical);
        msgBox.setText("Application is not connected to the database");
        msgBox.exec();
    }
    
    userDatabaseControl.closeDatabase();
    return pacioliQuery;
    

    }
    @

    There are other similar cases in my code where this happens. It appears when I try to delete a pointer or set a parent to the pointer.

    Would it cause trouble if I don't delete the pointer?

    I tried setModel(NULL) on closeEvent of the parent window but the warning just appeared twice.

    Is it safe to just delete the pointer and just ignore the warning?


  • Moderators

    since you set the parent on the model you don't need to worry about the the deletion. Qt does handle it for you in this case.


  • Moderators

    If you are setting a QObject parent, you do not need to delete the pointer yourself. It will be done automatically by Qt when the parent is deleted.



  • Yes, but that warning appears either when I set a parent or delete the pointer.

    I'm wondering if it's OK not to delete the pointer so the warning does not appear or set a parent/delete the pointer and ignore the warning.

    Thanks!


  • Moderators

    Try using deleteLater() instead of standard C++ deletion, maybe it will help here.


  • Moderators

    [quote author="puterk" date="1388657011"]Yes, but that warning appears either when I set a parent or delete the pointer.

    I'm wondering if it's OK not to delete the pointer so the warning does not appear or set a parent/delete the pointer and ignore the warning.
    [/quote]
    i see...
    The warning is rather connected to the database connection than the QObject relation.

    I think it's because you create a connection to the database every time you call User::getStatusList() and return a QSqlQuery object which still refers to the connection but is later used by your sql model?
    Why don't you just use "QSqlDatabase::addDatabase()":http://qt-project.org/doc/qt-4.8/qsqldatabase.html#addDatabase, like it's meant to be used anyway?



  • bq. The warning is rather connected to the database connection than the QObject relation.

    I have thought of that but I did not know what to do about it.

    bq. Why don’t you just use QSqlDatabase::addDatabase() [qt-project.org], like it’s meant to be used anyway?

    I am using a class for connecting to the database.
    Here is the cpp:

    @#include "databasecontrol.h"
    #include <QSqlQuery>
    #include <QDebug>

    QSqlDatabase qpaciolidb = QSqlDatabase::addDatabase("QPSQL");
    static QString hostName;
    static QString databaseName;
    static QString databaseUserName;
    static QString databasePassword;

    DatabaseControl::DatabaseControl()
    {

    }

    void DatabaseControl::setDatabaseInfo(QString inputHostName,
    QString inputDatabaseName,
    QString inputDatabaseUserName,
    QString inputDatabasePassword)
    {
    hostName = inputHostName;
    databaseName = inputDatabaseName;
    databaseUserName = inputDatabaseUserName;
    databasePassword = inputDatabasePassword;
    }

    QSqlDatabase DatabaseControl::connectToDatabase()
    {
    qpaciolidb.setHostName(hostName);
    qpaciolidb.setDatabaseName(databaseName);
    qpaciolidb.setUserName(databaseUserName);
    qpaciolidb.setPassword(databasePassword);

    return qpaciolidb;
    

    }

    bool DatabaseControl::checkDatabaseConnection()
    {

    if (qpaciolidb.open()==true)
    {
        return true;
    }
    else
    {
        return false;
    }
    

    }

    void DatabaseControl::closeDatabase()
    {
    qpaciolidb.close();
    }
    @

    Should I connect once and close the database when the application is closed? I'll appreciate any advice that you can give. Thanks!


Log in to reply
 

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