Qt MySQL adapter refuses to connect randomly: Unable to allocate a MYSQL
<p>I'm using QMYSQL to connect to a local database. The application runs over several threads. Each thread connects to database using an independent connection. Sometimes Qt throws following error when I try to connect to database. What's the problem?</p>

<pre><code>QMYSQL: Unable to allocate a MYSQL object
</code></pre>

<p><strong>Update</strong></p>

<p>Added the code used to connect. This object is moved to a thread, the connection is named. <code>critical</code> is a signal emitted to main window to terminate application execution after a critical error (showing a message). <code>log</code> is a signal that is emitted to log incidences into database. </p>

<pre><code>void ClientWorker::connect() {
    m_database = QSqlDatabase::addDatabase("QMYSQL","wsc");
    m_database.setHostName(m_host);
    m_database.setDatabaseName(m_databaseName);
    m_database.setPort(m_port);
    m_database.setUserName(m_db_username);
    m_database.setPassword(m_db_password);
    if(!m_database.open()) {
        QString error = "Unable to connect to database. Reason:\n";
        error+= m_database.lastError().text();
        log("Unable to connect to database! ", error, "ERROR" );
        emit critical(tr("Database Error!"),error);
    } else {
        log("Connected to datbase successfully.", "", "NOTICE" );
}
</code></pre>

<p><strong>Update 2</strong></p>

<p>I just realized that every time a connection is made out of main thread, (with no active connections in main tread) the driver fails to load. I just added a small dummy connection code in <code>main()</code> that connects and disconnects immediately (before any thread connects). Adding that code, everything works fine. I'm not sure why threads can't connect before a connection in main thread, but I think it looks like a bug. Hope this helps someone, Took 3 days of mine :/ </p>