I am continuing the work on my assignment involving hash tables in C++. I asked a question on here yesterday about the "proper way" to free memory since I had some issues, and valgrind was showing I leaked 17.000.000 bytes of memory.
I worked on the code all day today and I sort of fixed it I guess, and after running this command again
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --num-callers=20 --log-file=valgrind.log ./main
Valgrind
shows there are still some blocks that are reachable. After doing some research on Valgrind
FAQ page and this I found out that the possibly lost
section doesn't really matter that much, and it only refers to allocations that fit only the first definition of "memory leak". These blocks were not freed, but they could have been freed (if the programmer had wanted to) because the program still was keeping track of pointers to those memory blocks.
That made me think about 3 things.
Firstly, after reading the FAQ and the previous SO question I am assuming a real-life programmer "wouldn't care". I've heard about the military missile system leaking memory link and they never fixed it.
Secondly, this assignment is for my DataStructures class which "sort" of focuses on memory management (pointers for BST's, LL's,...). The professor is a quite strict grader but I am not sure if he runs valgrind
for every submitted assignment. The reason why I am saying this is because for the love of god I can't find the "still reachable" blocks in my code. It would save me a lot of time and trouble if I could simply avoid that.
Thirdly, I am not familiar with many programming languages, but I've heard of garbage collectors (Java, JS, C#). Is that what they essentially do? (free unused memory) Is the feature of not having a garbage collector in C++ a good or a bad thing?
I guess my final question is: How much (on a scale from 1 - 10) should I care about the still reachable blocks if I understand correctly, it's technically not a memory leak it's just poor memory management.
Thank you
Valgrind report(It's a lot bigger than this, I just pasted the "important part"):
==350555== LEAK SUMMARY:
==350555== definitely lost: 0 bytes in 0 blocks
==350555== indirectly lost: 0 bytes in 0 blocks
==350555== possibly lost: 0 bytes in 0 blocks
==350555== still reachable: 20,919 bytes in 292 blocks
==350555== suppressed: 0 bytes in 0 blocks
==350555==
==350555== For lists of detected and suppressed errors, rerun with: -s
==350555== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)