From b8325bb573842133998653a375e45f435ea3683c Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Fri, 6 Sep 2024 14:25:59 +0200 Subject: [PATCH] Fix memory leak --- src/Tour.cpp | 11 +++++++---- src/tsp.cpp | 10 +++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Tour.cpp b/src/Tour.cpp index 1e7a26f..22ad7de 100755 --- a/src/Tour.cpp +++ b/src/Tour.cpp @@ -50,10 +50,11 @@ void Tour::draw(QGraphicsScene *scene) const { return; Node *node = m_startNode; - while (node->next != nullptr) { + do{ + node->point.draw(scene); node->point.drawTo(node->next->point, scene); node = node->next; - } + } while (node != m_startNode); } int Tour::size() const { @@ -87,6 +88,7 @@ double Tour::distance() const { void Tour::insertNearest(Point p) { if (m_startNode == nullptr) { m_startNode = new Node(p); + m_startNode->next = m_startNode; // Make it cirkular return; } if (m_startNode->next == nullptr) { @@ -96,7 +98,7 @@ void Tour::insertNearest(Point p) { } Node *nearest = m_startNode; - double minDistance = INFINITY; + double minDistance = Q_INFINITY; Node *node = m_startNode; do { @@ -115,6 +117,7 @@ void Tour::insertNearest(Point p) { void Tour::insertSmallest(Point p) { if (m_startNode == nullptr) { m_startNode = new Node(p); + m_startNode->next = m_startNode; // Make it cirkular return; } if (m_startNode->next == nullptr) { @@ -124,7 +127,7 @@ void Tour::insertSmallest(Point p) { } Node *bestNode = m_startNode; - double minIncrease = INFINITY; + double minIncrease = Q_INFINITY; Node *node = m_startNode; do { diff --git a/src/tsp.cpp b/src/tsp.cpp index e8740ff..944a678 100755 --- a/src/tsp.cpp +++ b/src/tsp.cpp @@ -23,7 +23,7 @@ int main(int argc, char *argv[]) { filename.insert(0, "../../../"); #endif - ifstream input; + ifstream input; input.open(filename); // get dimensions @@ -48,10 +48,10 @@ int main(int argc, char *argv[]) { Point p(x, y); tour.insertNearest(p); //uncomment the 4 lines below to animate - //tour.draw(scene); - //std::chrono::milliseconds dura(150); - //std::this_thread::sleep_for(dura); - //a.processEvents(); + tour.draw(scene); + std::chrono::milliseconds dura(50); + std::this_thread::sleep_for(dura); + a.processEvents(); } input.close();