重新打开mysql连接会导致segfault

时间:2017-04-12 10:56:10

标签: c++ mysql qt qt5

当我尝试连接到同一个数据库两次时,简单的应用程序会导致段错误。它适用于本地数据库,但在第二次使用任何远程隧道调用open()时崩溃。可能是什么原因?我在做一些我不该做的事情吗?

显然,这是一个示例,但实际上我正在尝试为更大的应用程序管理数据库连接,并且管理员在请求时会执行类似的打开/关闭周期。

QT版本是5.7.0。从源代码编译以获得更好的堆栈跟踪但不确定它是否有帮助。

远程MySQL是5.6.27(实际是亚马逊RDS),本地是5.7.17。

这不是真的发生,因为我使用隧道。我可以创建一个到我的本地数据库的隧道,该应用程序工作正常,没有崩溃。

如果我将本地数据库隧道传输到远程计算机,然后从隧道返回到本地,则应用仍然有效。所以这也不是延迟。

代码:

#include <QCoreApplication>
#include <QSqlDatabase>

void initDb(QSqlDatabase db) {
    QString connectionHost = "127.0.0.1";
    int port = 3311; // local tunnel
    QString user = "mydbuser";
    QString password = "mydbpassword";
    QString database = "mydbname";

    db.setHostName(connectionHost);
    db.setPort(port);
    db.setUserName(user);
    db.setPassword(password);
    db.setDatabaseName(database);
}

int main(int argc, char *argv[]) {
    QString name1 = "db1", name2 = "db2";
    {
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", name1);
        initDb(db);
        db.open();
        db.close();
    }
    QSqlDatabase::removeDatabase(name1);

    {
        QSqlDatabase db1 = QSqlDatabase::addDatabase("QMYSQL", name2);
        initDb(db1);
        db1.open();
        db1.close();
    }
    QSqlDatabase::removeDatabase(name2);

    return 0;
}

跟踪:

Thread 1 (Thread 0x7f7603ee3740 (LWP 3505)):
#0  __GI___pthread_mutex_lock (mutex=0x20) at ../nptl/pthread_mutex_lock.c:67
    __PRETTY_FUNCTION__ = "__pthread_mutex_lock"
    type = <optimized out>
    id = <optimized out>
#1  0x00007f75ff228d79 in ?? () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20
No symbol table info available.
#2  0x00007f75ff1ff660 in ?? () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20
No symbol table info available.
#3  0x00007f75ff1d65e5 in mysql_real_connect () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20
No symbol table info available.
#4  0x00007f7603f06a54 in QMYSQLDriver::open (this=0xbd4c90, db=..., user=..., password=..., host=..., port=3311, connOpts=...) at ../../../sql/drivers/mysql/qsql_mysql.cpp:1393
    d = 0xbd5ea0
    optionFlags = 65536
    opts = {<QList<QString>> = {<QListSpecialMethods<QString>> = {<No data fields>}, {p = {static shared_null = {ref = {atomic = {_q_value = {<std::__atomic_base<int>> = {static _S_alignment = 4, _M_i = -1}, <No data fields>}}}, alloc = 0, begin = 0, end = 0, array = {0x0}}, d = 0x7f7603b0ffb0 <QListData::shared_null>}, d = 0x7f7603b0ffb0 <QListData::shared_null>}}, <No data fields>}
    unixSocket = {static null = {<No data fields>}, d = 0x7f7603b0c140 <QArrayData::shared_null>}
    sslCert = {static null = {<No data fields>}, d = 0x7f7603b0c140 <QArrayData::shared_null>}
    sslCA = {static null = {<No data fields>}, d = 0x7f7603b0c140 <QArrayData::shared_null>}
    sslKey = {static null = {<No data fields>}, d = 0x7f7603b0c140 <QArrayData::shared_null>}
    sslCAPath = {static null = {<No data fields>}, d = 0x7f7603b0c140 <QArrayData::shared_null>}
    sslCipher = {static null = {<No data fields>}, d = 0x7f7603b0c140 <QArrayData::shared_null>}
    reconnect = 0 '\000'
    connectTimeout = 0
    readTimeout = 0
    writeTimeout = 0
    __PRETTY_FUNCTION__ = "virtual bool QMYSQLDriver::open(const QString&, const QString&, const QString&, const QString&, int, const QString&)"
    mysql = 0x7ffe4d0aee80
#5  0x00007f7603f2d4b9 in QSqlDatabase::open (this=0x7ffe4d0aef70) at kernel/qsqldatabase.cpp:835
No locals.
#6  0x00000000004010d0 in main (argc=1, argv=0x7ffe4d0af098) at ../main.cpp:31
    db1 = {static defaultConnection = 0x7f7603f52bf5 "qt_sql_default_connection", d = 0xbbdee0}
    name1 = {static null = {<No data fields>}, d = 0xbbde50}
    name2 = {static null = {<No data fields>}, d = 0xbbde80}

0 个答案:

没有答案