Why I have a memory leak in this code? C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why I have a memory leak in this code? C++

I'm trying to do a method to insert data in a simple linked list in C++, I have a took called fsanitizer, that helps you to detect memory corruption, memory leaks, segmentation faults and more. When I have that tool activated, it throws me a "memory leak", I don't know how to solve it, here is the code: https://code.sololearn.com/cgs7cfwRlHL5/?ref=app#cpp

12th Oct 2019, 4:08 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
5 Answers
+ 1
Eduardo Perez Regin did you delete it in the correct place? I would do it after anchor = aux; You've probably tried that. You also need to set aux to a nullptr after you delete it: anchor = aux; delete aux; aux = nullptr; If that doesn't work, I would try using the Node type's destructor to free the memory.
12th Oct 2019, 3:41 PM
Zeke Williams
Zeke Williams - avatar
+ 1
Zeke I did that and it works, thank you so much!
19th Oct 2019, 7:38 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
0
delete aux maybe? It's the only place I can see that you've allocated dynamically
12th Oct 2019, 5:09 AM
Zeke Williams
Zeke Williams - avatar
0
Zeke, I tried that already, but the program crash 🤔, maybe that's not the only error I have, I'm trying to figure out what is going on, because if I use delete aux, the program doesn't work at all, but if I don't write it, it works (with memory leak)
12th Oct 2019, 5:39 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
12th Oct 2019, 6:45 AM
Infinity
Infinity - avatar