QSqlDatabase 数据库操作没有removeDatabase 导致程序崩溃(此文极有可能是错误的,仅作个人留档参考)

void QSqlDatabase::removeDatabase ( const QString & connectionName ) [static] Removes the database connection connectionName from the list of database connections. Warning: There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur. Example: // WRONG QSqlDatabase db = QSqlDatabase::database("sales"); QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db); QSqlDatabase::removeDatabase("sales"); // will output a warning // "db" is now a dangling invalid database connection, // "query" contains an invalid result set The correct way to do it: { QSqlDatabase db = QSqlDatabase::database("sales"); QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db); } // Both "db" and "query" are destroyed because they are out of scope QSqlDatabase::removeDatabase("sales"); // correct To remove the default connection, which may have been created with a call to addDatabase() not specifying a connection name, you can retrieve the default connection name by calling connectionName() on the database returned by database(). Note that if a default database hasn't been created an invalid database will be returned.

以上是查阅文档看到的说明

如果在数据库连接使用完毕以后  没有移除数据库   将会可能导致内存泄漏

今天就遇到了多次打开数据库中的多个表  而每次第一二次打开都没问题  关闭子窗口后再次打开就崩溃

debug提示信息

QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.

 

 

删除的一个实例代码

QString dataName; { dataName = QSqlDatabase::database().connectionName(); }//超出作用域,隐含对象QSqlDatabase::database()被删除。 QSqlDatabase::removeDatabase(dataName);

你可能感兴趣的:(数据库,function,database,query,leak,output)