Fix memory leak

This commit is contained in:
Love 2024-09-06 14:25:59 +02:00
parent 93553a8081
commit 7fc0599946
2 changed files with 12 additions and 9 deletions

View File

@ -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 {

View File

@ -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();