Implement a proper destructor for ComputerManager to prevent polling threads from living beyond QGuiApplication

This commit is contained in:
Cameron Gutman 2018-07-18 21:27:43 -07:00
parent 06501bd4b7
commit 5ee1358712
2 changed files with 52 additions and 0 deletions

View file

@ -286,6 +286,56 @@ ComputerManager::ComputerManager(QObject *parent)
settings.endArray();
}
ComputerManager::~ComputerManager()
{
QWriteLocker lock(&m_Lock);
// Delete machines that haven't been resolved yet
while (!m_PendingResolution.isEmpty()) {
MdnsPendingComputer* computer = m_PendingResolution.first();
delete computer;
m_PendingResolution.removeFirst();
}
// Delete the browser to stop discovery
delete m_MdnsBrowser;
m_MdnsBrowser = nullptr;
{
QMutableMapIterator<QString, QThread*> i(m_PollThreads);
// Interrupt all polling threads
while (i.hasNext()) {
i.next();
i.value()->requestInterruption();
}
// Wait for all threads to terminate before destroying
// the NvComputer objects.
i.toFront();
while (i.hasNext()) {
i.next();
QThread* thread = i.value();
thread->wait();
delete thread;
i.remove();
}
}
{
QMutableMapIterator<QString, NvComputer*> i(m_KnownHosts);
// Destroy all NvComputer objects
while (i.hasNext()) {
i.next();
delete i.value();
i.remove();
}
}
}
void ComputerManager::saveHosts()
{
QSettings settings;

View file

@ -249,6 +249,8 @@ class ComputerManager : public QObject
public:
explicit ComputerManager(QObject *parent = nullptr);
virtual ~ComputerManager();
Q_INVOKABLE void startPolling();
Q_INVOKABLE void stopPollingAsync();