Fix memory leak

This commit is contained in:
Love 2024-09-06 14:25:59 +02:00
parent 46e0756949
commit b8325bb573
2 changed files with 12 additions and 9 deletions

View File

@ -50,10 +50,11 @@ void Tour::draw(QGraphicsScene *scene) const {
return; return;
Node *node = m_startNode; Node *node = m_startNode;
while (node->next != nullptr) { do{
node->point.draw(scene);
node->point.drawTo(node->next->point, scene); node->point.drawTo(node->next->point, scene);
node = node->next; node = node->next;
} } while (node != m_startNode);
} }
int Tour::size() const { int Tour::size() const {
@ -87,6 +88,7 @@ double Tour::distance() const {
void Tour::insertNearest(Point p) { void Tour::insertNearest(Point p) {
if (m_startNode == nullptr) { if (m_startNode == nullptr) {
m_startNode = new Node(p); m_startNode = new Node(p);
m_startNode->next = m_startNode; // Make it cirkular
return; return;
} }
if (m_startNode->next == nullptr) { if (m_startNode->next == nullptr) {
@ -96,7 +98,7 @@ void Tour::insertNearest(Point p) {
} }
Node *nearest = m_startNode; Node *nearest = m_startNode;
double minDistance = INFINITY; double minDistance = Q_INFINITY;
Node *node = m_startNode; Node *node = m_startNode;
do { do {
@ -115,6 +117,7 @@ void Tour::insertNearest(Point p) {
void Tour::insertSmallest(Point p) { void Tour::insertSmallest(Point p) {
if (m_startNode == nullptr) { if (m_startNode == nullptr) {
m_startNode = new Node(p); m_startNode = new Node(p);
m_startNode->next = m_startNode; // Make it cirkular
return; return;
} }
if (m_startNode->next == nullptr) { if (m_startNode->next == nullptr) {
@ -124,7 +127,7 @@ void Tour::insertSmallest(Point p) {
} }
Node *bestNode = m_startNode; Node *bestNode = m_startNode;
double minIncrease = INFINITY; double minIncrease = Q_INFINITY;
Node *node = m_startNode; Node *node = m_startNode;
do { do {

View File

@ -23,7 +23,7 @@ int main(int argc, char *argv[]) {
filename.insert(0, "../../../"); filename.insert(0, "../../../");
#endif #endif
ifstream input; ifstream input;
input.open(filename); input.open(filename);
// get dimensions // get dimensions
@ -48,10 +48,10 @@ int main(int argc, char *argv[]) {
Point p(x, y); Point p(x, y);
tour.insertNearest(p); tour.insertNearest(p);
//uncomment the 4 lines below to animate //uncomment the 4 lines below to animate
//tour.draw(scene); tour.draw(scene);
//std::chrono::milliseconds dura(150); std::chrono::milliseconds dura(50);
//std::this_thread::sleep_for(dura); std::this_thread::sleep_for(dura);
//a.processEvents(); a.processEvents();
} }
input.close(); input.close();