From eb0aa0b76fabcff044d3ee9e4abe253b5484cdca Mon Sep 17 00:00:00 2001
From: hjk <hjk@qt.io>
Date: Fri, 7 Jul 2017 16:40:10 +0200
Subject: [PATCH] Debugger: Connect GdbServerPortsGatherer signals in
 constructor

Not in start(), when it would add extra connections on each re-run.

Change-Id: I5fe9e047a188276f3bde54227dc0509ab77366c4
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
---
 src/plugins/debugger/debuggerruncontrol.cpp | 47 +++++++++++----------
 src/plugins/debugger/debuggerruncontrol.h   |  3 +-
 2 files changed, 27 insertions(+), 23 deletions(-)

diff --git a/src/plugins/debugger/debuggerruncontrol.cpp b/src/plugins/debugger/debuggerruncontrol.cpp
index cb56f3e47f7..4bbfd93b9f6 100644
--- a/src/plugins/debugger/debuggerruncontrol.cpp
+++ b/src/plugins/debugger/debuggerruncontrol.cpp
@@ -702,6 +702,11 @@ GdbServerPortsGatherer::GdbServerPortsGatherer(RunControl *runControl)
     : RunWorker(runControl)
 {
     setDisplayName("GdbServerPortsGatherer");
+
+    connect(&m_portsGatherer, &DeviceUsedPortsGatherer::error,
+            this, &RunWorker::reportFailure);
+    connect(&m_portsGatherer, &DeviceUsedPortsGatherer::portListReady,
+            this, &GdbServerPortsGatherer::handlePortListReady);
 }
 
 GdbServerPortsGatherer::~GdbServerPortsGatherer()
@@ -711,31 +716,29 @@ GdbServerPortsGatherer::~GdbServerPortsGatherer()
 void GdbServerPortsGatherer::start()
 {
     appendMessage(tr("Checking available ports..."), NormalMessageFormat);
-    connect(&m_portsGatherer, &DeviceUsedPortsGatherer::error, this, [this](const QString &msg) {
-        reportFailure(msg);
-    });
-    connect(&m_portsGatherer, &DeviceUsedPortsGatherer::portListReady, this, [this] {
-        Utils::PortList portList = device()->freePorts();
-        appendMessage(tr("Found %1 free ports").arg(portList.count()), NormalMessageFormat);
-        if (m_useGdbServer) {
-            m_gdbServerPort = m_portsGatherer.getNextFreePort(&portList);
-            if (!m_gdbServerPort.isValid()) {
-                reportFailure(tr("Not enough free ports on device for C++ debugging."));
-                return;
-            }
-        }
-        if (m_useQmlServer) {
-            m_qmlServerPort = m_portsGatherer.getNextFreePort(&portList);
-            if (!m_qmlServerPort.isValid()) {
-                reportFailure(tr("Not enough free ports on device for QML debugging."));
-                return;
-            }
-        }
-        reportStarted();
-    });
     m_portsGatherer.start(device());
 }
 
+void GdbServerPortsGatherer::handlePortListReady()
+{
+    Utils::PortList portList = device()->freePorts();
+    appendMessage(tr("Found %1 free ports").arg(portList.count()), NormalMessageFormat);
+    if (m_useGdbServer) {
+        m_gdbServerPort = m_portsGatherer.getNextFreePort(&portList);
+        if (!m_gdbServerPort.isValid()) {
+            reportFailure(tr("Not enough free ports on device for C++ debugging."));
+            return;
+        }
+    }
+    if (m_useQmlServer) {
+        m_qmlServerPort = m_portsGatherer.getNextFreePort(&portList);
+        if (!m_qmlServerPort.isValid()) {
+            reportFailure(tr("Not enough free ports on device for QML debugging."));
+            return;
+        }
+    }
+    reportStarted();
+}
 
 // GdbServerRunner
 
diff --git a/src/plugins/debugger/debuggerruncontrol.h b/src/plugins/debugger/debuggerruncontrol.h
index c22a4c969f5..dc3b7a648a3 100644
--- a/src/plugins/debugger/debuggerruncontrol.h
+++ b/src/plugins/debugger/debuggerruncontrol.h
@@ -111,7 +111,8 @@ public:
     Utils::Port qmlServerPort() const { return m_qmlServerPort; }
 
 private:
-    void start();
+    void start() override;
+    void handlePortListReady();
 
     ProjectExplorer::DeviceUsedPortsGatherer m_portsGatherer;
     bool m_useGdbServer = false;
-- 
GitLab