#include #include #include struct Cfg { QString drv, qname, host; int port; QString name, user, pass; }; void createDestroyDb(const Cfg& c) { bool ok = false; QSqlDatabase d; for (;;) { d = QSqlDatabase::addDatabase(c.drv, c.qname); if (!d.isValid()) break; d.setHostName(c.host); d.setPort(c.port); d.setDatabaseName(c.name); d.setUserName(c.user); d.setPassword(c.pass); { /*static QMutex m; QMutexLocker l(&m);*/ ok = d.open(); } break; } // The QMutex hack suppresses the bug. if (!ok) qWarning().noquote() << d.lastError().text(); d = {}; QSqlDatabase::removeDatabase(c.qname); } void f() { std::vector v; for (int i = 0; i < 1000; ++i) { v.push_back("db" + QString::number(i)); } QtConcurrent::blockingMap(v, [](const QString& s) { createDestroyDb({ "QPSQL", s, "wronghost", 5432, "db", "user", "pass" }); } ); qInfo("No crash happened."); } int main(int ac, char* av[]) { qSetMessagePattern("[%{if-warning} - - - WARN %{endif}%{time HHmmss.zzz} %{file}:%{line} %{function} %{threadid}] %{message}"); QCoreApplication app(ac, av); QTimer::singleShot(1000, [&] { f(); app.quit(); }); return app.exec(); }