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
5 Respostas
+ 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.
+ 1
Zeke I did that and it works, thank you so much!
0
delete aux maybe? It's the only place I can see that you've allocated dynamically
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)
0
Might help?
https://stackoverflow.com/q/20376913/9903084