title: Qt5.2.0; Debian Wheezy: QSqlDatabase destructor causes segfault
body: <p>I have an app that utilizes a wrapper class to access a MySQL database.  Since the database is accessed at various places in the app, and the app is multithreaded, the wrapper class is used to minimize the amount of repetitive code needed to access the DB.  </p>
  
  <p>I read somewhere that a <code>QSqlDatabase</code> must be constructed, used and then destructed within the same thread.  Undocumented/unsupported behavior may occur if you create the connection in one thread, then destruct it in another.  To prevent this, the wrapper creates the connection just before setting up the <code>QSqlQuery</code>, and then closes and removes that connection when the query is done.  Setting up a pool of connections and keeping them alive throughout app execution isn't really feasible because there are any number of threads running at any given time, each of which may need to do multiple DB tasks asynchrously, so there isn't exactly a 1-1 correlation between threads and database connections to begin with.</p>
  
  <p>The problem is that after the app has run for a while, it crashes with a segfault which blames the <code>QSqlDatabase</code> destructor.  Odd thing is that the error only shows up after the app has been running for a while and has done numerous queries.  (Sometimes it crashes after 15 minutes, other times after a few hours).  Most recent stack trace: </p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  pthread_cond_timedwait@@GLIBC_2.3.2 ()
      at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S:174
  174     ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S: No such file or     directory.
  (gdb) where
  #0  pthread_cond_timedwait@@GLIBC_2.3.2 ()
      at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S:174
  #1  0x00007ffff32f55b4 in my_thread_global_end ()
     from /opt/Qt/5.2.0/gcc_64/plugins/sqldrivers/libqsqlmysql.so
  #2  0x00007ffff32f40a5 in my_end ()
     from /opt/Qt/5.2.0/gcc_64/plugins/sqldrivers/libqsqlmysql.so
  #3  0x00007ffff32db5c7 in mysql_server_end ()
     from /opt/Qt/5.2.0/gcc_64/plugins/sqldrivers/libqsqlmysql.so
  #4  0x00007ffff32cd806 in ?? ()
     from /opt/Qt/5.2.0/gcc_64/plugins/sqldrivers/libqsqlmysql.so
  #5  0x00007ffff32cd829 in ?? ()
     from /opt/Qt/5.2.0/gcc_64/plugins/sqldrivers/libqsqlmysql.so
  #6  0x00007ffff7657537 in ?? () from /opt/Qt/5.2.0/gcc_64/lib/libQt5Sql.so.5
  #7  0x00007ffff7657e7d in QSqlDatabase::~QSqlDatabase() ()
     from /opt/Qt/5.2.0/gcc_64/lib/libQt5Sql.so.5
  #8  0x00007ffff7659204 in ?? () from /opt/Qt/5.2.0/gcc_64/lib/libQt5Sql.so.5
  #9  0x00000000004143c2 in DB::~DB (this=0x7ffff0829200,
      __in_chrg=&lt;optimized out&gt;) at db.cpp:32
  #10 0x000000000042e758 in WeatherOutcome::MassInsert (
      this=0x7ffff0829550, inputVector=...) at weatheroutcome.cpp:96
  </code></pre>
  
  <p>Here's the wrapper class header file:</p>
  
  <pre><code>#ifndef DB_H
  #define DB_H
  #include &lt;QString&gt;
  #include &lt;QtSql&gt; 
  #include &lt;QSqlQuery&gt;
  #include &lt;QSqlError&gt;
  #include &lt;QUuid&gt;
  class DB
  {
  public:
      DB();
      ~DB();
      bool SetQuery(QString query);
      bool Exec();
      void manualConnect();
  protected:
      QSqlQuery query;
      QSqlDatabase* db;
  };
  
  #endif // DB_H
  </code></pre>
  
  <p>Wrapper class CPP File:</p>
  
  <pre><code>#include db.h
  DB::DB()
  {
  
  }
  DB::~DB()
  {
      if (query.isActive())
      {
          query.finish();
          query.clear();
      }
      if (db != NULL)
      {
          QString connName = db-&gt;connectionName();
          db-&gt;close();
          delete db;
          db = NULL;
          try
          {
              QSqlDatabase::removeDatabase(connName);
          }
  
  
          catch(...)
          {
  
          }
      }
  }
  
  void DB::manualConnect()
  {
      QUuid uniqueId = QUuid::createUuid();
      QString connectionID = uniqueId.toString();
      QSqlDatabase::addDatabase("QMYSQL", connectionID);
  
      db = new QSqlDatabase(QSqlDatabase::database(connectionID, false));
      db-&gt;setHostName("127.0.0.1");
      db-&gt;setDatabaseName("my_db_name");
      db-&gt;setUserName("username");
      db-&gt;setPassword("password");
  
      db-&gt;open();
  }
  
  bool DB::Exec()
  {
      return query.exec()  ;
  }
  
  bool DB::SetQuery(QString queryString)
  {
      manualConnect();
      if (db)
      {
          if (db-&gt;isOpen())
          {
              query = QSqlQuery(*db);
              return query.prepare(queryString);
          }
          return false;
      }
      else
          return false;
  }
  </code></pre>
  
  <p>Why would this rather simple destructor cause a segfault and crash the app?</p>