@@ -140,12 +140,14 @@ QSqlDatabase QgsAuthManager::authDatabaseConnection() const
140140 // triggers a condition in QSqlDatabase which detects the nullptr private thread data and returns an invalid database instead.
141141 // QSqlDatabase::removeDatabase is thread safe, so this is ok to do.
142142 // Right about now is a good time to re-evaluate your selected career ;)
143- connect ( QThread::currentThread (), &QThread::finished, QThread::currentThread (), [connectionName, this ]
143+ QMetaObject::Connection connection = connect ( QThread::currentThread (), &QThread::finished, QThread::currentThread (), [connectionName, this ]
144144 {
145145 QMutexLocker locker ( mMutex );
146- QgsDebugMsgLevel ( QStringLiteral ( " Removing outdated connection to %1 on thread exit" ).arg ( connectionName ), 2 );
147146 QSqlDatabase::removeDatabase ( connectionName );
147+ mConnectedThreads .remove ( QThread::currentThread () );
148148 }, Qt::DirectConnection );
149+
150+ mConnectedThreads .insert ( QThread::currentThread (), connection );
149151 }
150152 }
151153 else
@@ -2961,6 +2963,15 @@ void QgsAuthManager::tryToStartDbErase()
29612963
29622964QgsAuthManager::~QgsAuthManager ()
29632965{
2966+ QMutexLocker locker ( mMutex );
2967+ QMapIterator<QThread *, QMetaObject::Connection> iterator ( mConnectedThreads );
2968+ while ( iterator.hasNext () )
2969+ {
2970+ iterator.next ();
2971+ iterator.key ()->disconnect ( iterator.value () );
2972+ }
2973+ locker.unlock ();
2974+
29642975 if ( !isDisabled () )
29652976 {
29662977 delete QgsAuthMethodRegistry::instance ();
0 commit comments