Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. "Unable to free statement: connection pointer is NULL" - This warning appears when a pointer is deleted.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 6.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    puterk
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        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.

        (Z(:^

        1 Reply Last reply
        0
        • P Offline
          P Offline
          puterk
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

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

            (Z(:^

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              [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?

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • P Offline
                P Offline
                puterk
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved