PDA

View Full Version : Crash while connecting to MySQL



vfernandez
9th January 2007, 11:19
I have a thread that opens a MySQL connection, executes a SELECT query, then closes the connection. Sometimes it crashes with a segmentation fault while trying to establish the connection. This is the backtrace I get:


Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1246762096 (LWP 5786)]
0xb51e232e in my_stat () from /usr/lib/libmysqlclient_r.so.15
(gdb) bt
#0 0xb51e232e in my_stat () from /usr/lib/libmysqlclient_r.so.15
#1 0xb51dad95 in escape_quotes_for_mysql () from /usr/lib/libmysqlclient_r.so.15
#2 0xb51db675 in get_charsets_dir () from /usr/lib/libmysqlclient_r.so.15
#3 0xb51db7a9 in get_charset_by_csname () from /usr/lib/libmysqlclient_r.so.15
#4 0xb51f80e9 in mysql_set_character_set () from /usr/lib/libmysqlclient_r.so.15
#5 0xb6e96652 in QMYSQLDriver::open (this=0x81308c0, db=@0x80e6638, user=@0x80e663c, password=@0x80e6640, host=@0x80e6644, port=3306, connOpts=@0x80e6650)
at ../../../sql/drivers/mysql/qsql_mysql.cpp:1091
#6 0xb7ebb28e in QSqlDatabase::open (this=0xb5afe2d0) at kernel/qsqldatabase.cpp:792
#7 0x0806278f in DbThread::run (this=0x80e0480) at dbthread.cpp:61
#8 0xb72b0c09 in QThreadPrivate::start (arg=0x80e0480) at thread/qthread_unix.cpp:146
#9 0xb724e112 in start_thread () from /lib/libpthread.so.0
#10 0xb70c32ee in clone () from /lib/libc.so.6

My code is like this:


void DbThread::run()
{
QString connectionName = "DbThread-" + QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");

{
// Connect to the database server
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", connectionName);
db.setHostName(m_host);
db.setPort(m_port);
db.setDatabaseName(m_database);
db.setUserName(m_userName);
db.setPassword(m_password);
bool ok = db.open();
if(!ok) {
emit error(QString(tr("Error connecting to the database:\n\n%1")).arg(db.lastError().text()));
return;
}

QSqlQuery query(db);
query.exec("SELECT ...");
if(query.next()) {
...
emit resultsOfMyQuery(...);
}
db.close();
}

QSqlDatabase::removeDatabase(connectionName);
}

It seems to be due to the MySQL client library but I wanted to ask here before going to the MySQL forums. Anyone had this problem and was able to solve it?

I'm using Qt 4.2.2 under openSUSE 10.2, although I also compiled it under Windows for other clients and it worked perfectly there.

e8johan
9th January 2007, 13:05
Make sure that you keep all references to the QtSql module in one thread. It is very sensitive for this as the threading docs mention.