I can't seem to figure out why my code is leaking memory, I think I have implemented recursion correctly, take a look at my unload code. EDIT : I tried running my file with large dictionary and using it I am getting segmentation fault, I can't figure out why.

EDIT 2 : It was a silly mistake of my part, just used a different variable than the one I should have.

  • How do you know that the memory leak doesn't originate elsewhere? Can you please post the complete vargrind report? – Cliff B Sep 21 '19 at 23:02
  • I think something else is wrong with my code and not the unload function because now I am getting segmentation fault when I use the large dictionary, I have now posted the complete code, you can check it out. – Somit Jain Sep 21 '19 at 23:12
  • Also here is the valgrind report for the smaller dictionary HEAP SUMMARY: ==26546== in use at exit: 2,464 bytes in 11 blocks ==26546== total heap usage: 17 allocs, 6 frees, 13,008 bytes allocated ==26546== ==26546== 2,464 (224 direct, 2,240 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2 ==26546== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==26546== by 0x4011A2: load (dictionary.c:72) ==26546== by 0x400954: main (speller.c:40) – Somit Jain Sep 21 '19 at 23:14
  • One more thing, the problem has something to do with the unload function, because when I removed it and made it always return false, there was no segmentation fault, the code found all the misspelled words but in the end stopped because it could not unload. – Somit Jain Sep 21 '19 at 23:34
  • First of all, a seg fault is something totally different than a memory leak. Next, @curiouskiwi beat me to the punch by about 8 minutes. :-/ However, fixing errors will often fix memory leaks too. You may still have some memory issues. Also, I noticed that the code doesn't initialize nodes (and all the pointers inside them) when created. – Cliff B Sep 22 '19 at 1:00

What's n meant to represent? Your recursive function is looping from 0 to n to unload the children, but n isn't the number of children, N is.

  • I made such a silly mistake, and because n was declared to calculate the number of words in dictionary, clang did not gave me an error. Maybe I should start using better variable names. Anyway thanks. – Somit Jain Sep 22 '19 at 6:00
  • Better variable names is a good idea. :) – curiouskiwi Sep 22 '19 at 19:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .