QSqlQueryModel与父 - 应用程序崩溃

时间:2014-09-04 07:54:30

标签: qt postgresql qt5 qtsql qsqlquery

我对Qt很新,也许这就是为什么我无法完全理解孩子 - 父母的概念。我需要执行一些SQL查询。我设置了QSqlQuery,执行"准备和绑定"操作并执行它。接下来,我将它传递给模型并显示数据。关闭窗口时会出现问题 - 我收到内存冲突错误。仅当我使用父级创建模型时才会发生错误。这是代码:

QSqlQuery query;
query.prepare(QString("SELECT \
        %1 as nazwa \
        , kontrahentid \
        FROM kontrahent WHERE %2 ilike ?"
    ).arg(showWhat, searchBy) //handled above, no need to escape
);
query.addBindValue(searchString); //user input data - so bind it

if (!query.exec()) {
    qDebug() << query.lastError();
    QApplication::restoreOverrideCursor();
    return;
}

if (model == NULL)
//  model = new QSqlQueryModel; // app closes the window correctly
    model = new QSqlQueryModel(this); // app crashes when closing the window

model->setQuery(query);
if (model->lastError().isValid()) {
    qDebug() << model->lastError();
    QApplication::restoreOverrideCursor();
    return;
}

model->setHeaderData(0, Qt::Horizontal, "ID");
ui.kontrahenciList->setModel(model);
//ui.kontrahenciList->setModelColumn(1);
ui.kontrahenciList->show();

这是我得到的错误:

Unhandled exception at 0x0fe29f9a (qsqlpsqld.dll) in HurBudClientGUI.exe: 0xC0000005: Access violation reading location 0x00000004.

和调用堆栈:

qsqlpsqld.dll!QScopedPointer<QObjectData,QScopedPointerDeleter<QObjectData> >::data()  Line 143 + 0x3 bytes C++
qsqlpsqld.dll!qGetPtrHelper<QScopedPointer<QObjectData,QScopedPointerDeleter<QObjectData> > >(const QScopedPointer<QObjectData,QScopedPointerDeleter<QObjectData> > & p)  Line 919 + 0xb bytes  C++
qsqlpsqld.dll!QPSQLDriver::d_func()  Line 106 + 0x13 bytes  C++
qsqlpsqld.dll!QPSQLResultPrivate::privDriver()  Line 212    C++
qsqlpsqld.dll!QPSQLResultPrivate::deallocatePreparedStmt()  Line 306 + 0xc bytes    C++
qsqlpsqld.dll!QPSQLResult::~QPSQLResult()  Line 328 C++
qsqlpsqld.dll!QPSQLResult::`scalar deleting destructor'()  + 0xf bytes  C++
Qt5Sqld.dll!QSqlQueryPrivate::~QSqlQueryPrivate()  Line 94 + 0x23 bytes C++
Qt5Sqld.dll!QSqlQueryPrivate::`scalar deleting destructor'()  + 0xf bytes   C++
Qt5Sqld.dll!QSqlQuery::~QSqlQuery()  Line 245 + 0x1e bytes  C++
Qt5Sqld.dll!QSqlQueryModelPrivate::~QSqlQueryModelPrivate()  Line 90 + 0x3d bytes   C++
Qt5Sqld.dll!QSqlQueryModelPrivate::`scalar deleting destructor'()  + 0xf bytes  C++
Qt5Cored.dll!672cbf06()     
[Frames below may be incorrect and/or missing, no symbols loaded for Qt5Cored.dll]  
Qt5Cored.dll!672cb92a()     
Qt5Cored.dll!672c03f4()     
Qt5Cored.dll!67200dc4()     
Qt5Cored.dll!67203608()     
Qt5Sqld.dll!QSqlQueryModel::~QSqlQueryModel()  Line 175 + 0x9 bytes C++

正如我上面提到的:当(下面的一个):

时,错误不会发生
  • 我创建没有父级的QSqlQueryModel(model = new QSqlQueryModel;)
  • 我通过&#34;静电&#34;查询到QSqlQueryModel(无论是否有父)。

例如:

model->setQuery(
    QSqlQuery(
        QString("SELECT \
            %1 as nazwa \
            , kontrahentid \
            FROM kontrahent"
        ).arg(showWhat)
    )
);

我做错了什么? 真正的问题是:QSqlQueryModel有父母的目的是什么?如果我在窗口的析构函数中手动删除它 - 是否有任何差异?

我想这是一个错误 - 我在qt bugtracker上报告了它: https://bugreports.qt.io/browse/QTBUG-43889

3 个答案:

答案 0 :(得分:1)

这是关键部分:

if (model == NULL)
    //  model = new QSqlQueryModel; // app closes the window correctly
    model = new QSqlQueryModel(this); // app crashes when closing the window

Qt中的父子概念提供了许多功能,自动子销毁就是其中之一。

如果任何QObject将其他QObject设置为父级,则删除父级QObject时,子对象也将被删除。

现在,您提到您明确删除了窗口析构函数中的model。你不应该。如果您将this定义为父级,则model将与this一起自动删除,但您已在析构函数中将其删除,因此会删除model两次,因此出现错误。

答案 1 :(得分:0)

所以我删除了析构函数中的“删除操作”代码。错误仍然发生,唯一的区别是不同的调用堆栈。 错误讯息:

Unhandled exception at 0x0f249f9a (qsqlpsqld.dll) in HurBudClientGUI.exe: 0xC0000005: Access violation reading location 0x00000004.

调用堆栈:

qsqlpsqld.dll!QScopedPointer<QObjectData,QScopedPointerDeleter<QObjectData> >::data()  Line 143 + 0x3 bytes C++
qsqlpsqld.dll!qGetPtrHelper<QScopedPointer<QObjectData,QScopedPointerDeleter<QObjectData> > >(const QScopedPointer<QObjectData,QScopedPointerDeleter<QObjectData> > & p)  Line 919 + 0xb bytes  C++
qsqlpsqld.dll!QPSQLDriver::d_func()  Line 106 + 0x13 bytes  C++
qsqlpsqld.dll!QPSQLResultPrivate::privDriver()  Line 212    C++
qsqlpsqld.dll!QPSQLResultPrivate::deallocatePreparedStmt()  Line 306 + 0xc bytes    C++
qsqlpsqld.dll!QPSQLResult::~QPSQLResult()  Line 328 C++
qsqlpsqld.dll!QPSQLResult::`scalar deleting destructor'()  + 0xf bytes  C++
Qt5Sqld.dll!QSqlQueryPrivate::~QSqlQueryPrivate()  Line 94 + 0x23 bytes C++
Qt5Sqld.dll!QSqlQueryPrivate::`scalar deleting destructor'()  + 0xf bytes   C++
Qt5Sqld.dll!QSqlQuery::~QSqlQuery()  Line 245 + 0x1e bytes  C++
Qt5Sqld.dll!QSqlQueryModelPrivate::~QSqlQueryModelPrivate()  Line 90 + 0x3d bytes   C++
Qt5Sqld.dll!QSqlQueryModelPrivate::`scalar deleting destructor'()  + 0xf bytes  C++
Qt5Cored.dll!QScopedPointerDeleter<QObjectData>::cleanup(QObjectData * pointer)  Line 62 + 0x20 bytes   C++
Qt5Cored.dll!QScopedPointer<QObjectData,QScopedPointerDeleter<QObjectData> >::~QScopedPointer<QObjectData,QScopedPointerDeleter<QObjectData> >()  Line 109 + 0x9 bytes  C++
Qt5Cored.dll!QObject::~QObject()  Line 940 + 0x15 bytes C++
Qt5Cored.dll!QAbstractItemModel::~QAbstractItemModel()  Line 1454 + 0xf bytes   C++
Qt5Cored.dll!QAbstractTableModel::~QAbstractTableModel()  Line 3299 + 0x8 bytes C++
Qt5Sqld.dll!QSqlQueryModel::~QSqlQueryModel()  Line 175 + 0x9 bytes C++
HurBudClientGUI.exe!QSqlQueryModel::`scalar deleting destructor'()  + 0x10 bytes    C++
Qt5Cored.dll!QObjectPrivate::deleteChildren()  Line 1841 + 0x24 bytes   C++
Qt5Widgetsd.dll!QWidget::~QWidget()  Line 1488  C++
Qt5Widgetsd.dll!QDockWidget::~QDockWidget()  Line 1172 + 0x22 bytes C++
HurBudClientGUI.exe!searchDock::~searchDock()  Line 28 + 0x1c bytes C++

searchDock是我正在上课的课程。

答案 2 :(得分:0)

我想我解决了它。将模型初始化为视图的子项时:

model = new QSqlQueryModel(this);

问题在于操作顺序。当我关闭连接并删除我的类的析构函数中的数据库时 - 数据库断开连接,无法进一步操作。但是在我的类'析构函数之后,它的基类的析构函数依次进行操作 - 按照继承的顺序。执行QObject :: ~QObject()时,它会到达

QObjectPrivate::deleteChildren()

方法。然后,它最终会删除模型。该模型想要释放资源 - 这意味着QSqlResult(在这种情况下QPSQLResult是特定的)。它看起来如何:

QPSQLResult::~QPSQLResult()
{
    Q_D(QPSQLResult);
    cleanup();
    if (d->preparedQueriesEnabled && !d->preparedStmtId.isNull())
        d->deallocatePreparedStmt();
};

所以这里Qt试图解除分配prepareStatement - 无论事实如何,连接不再存在:

void QPSQLResultPrivate::deallocatePreparedStmt()
{
    const QString stmt = QLatin1String("DEALLOCATE ") + preparedStmtId;
    PGresult *result = privDriver()->exec(stmt);
    if (PQresultStatus(result) != PGRES_COMMAND_OK)
        qWarning("Unable to free statement: %s", PQerrorMessage(privDriver()->connection));
    PQclear(result);
    preparedStmtId.clear();
};

所以 - 为了让它正常工作,我必须打电话

QSqlQueryModel::~QSqlQueryModel()

QSqlQueryModel::clear()

关闭与DB的连接之前。我仍然认为这是一个错误。