0

------------------EDIT----------------

Everything is working fine, except for this one thing. My solution is identical to the staff solution, albeit quite a bit slower, but that's another topic. I don't feel comfortable posting my whole code.

In my load function:

valgrind --leak-check=full

Is giving me errors in the following line:

node* newNode = malloc(sizeof(node));

The error is:

==12543== 105,314,048 (32,057,312 direct, 73,256,736 indirect) bytes in 143,113 blocks are definitely lost in loss record 6 of 6
==12543==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12543==    by 0x401332: load (dictionary.c:134)
==12543==    by 0x40090D: main (speller.c:45)

Obviously, newNode is a temporary node I create each time a node needs to be added. I have tried free(newNode), but it always gives me segmentation faults. Given the lack of valgrind's complaining about other things, I assume the actual trie itself is successfully unloaded.

----------------END EDIT--------------

I have a temp node I create and malloc for traversing through my trie.

I then do what I need to, assign the temp as the child, set the temp node to null and free it with free(temp).

valgrind --leak-check=full points to the malloc line of this node and says there's a leak. Regular valgrind detects nothing, and the rest of my code works.

Any ideas?

2

It turned out one of my problems was I was freeing the root node after the recursive call, but the call itself frees the node, so that was causing a double free error.

The actual error was that for the last letter, I was creating a new node as well as an ending node, and the new node was not getting picked up by the unload function.

0

You say that you "have a temp node I create and malloc". I assume that you mean that you are creating a temp node pointer and mallocing space to it to traverse the existing trie. That's the first red flag. You should not be mallocing anything in unload(). It's very likely that you malloc the memory and then reassign the pointer to the trie, which immediately abandons the newly malloc'd memory and creates a memory leak.

Creating a pointer to a node and mallocing memory for it are two very distinct operations. Certainly, you may want to create a node pointer to traverse the trie, but you do NOT need to malloc space to it, and almost certainly shouldn't. You would assign the address of the root node to the pointer and update it as you traverse the trie though.

If you are setting the temp pointer to null and then calling free(temp), then you are freeing nothing. If the pointer points at null, what memory would the computer free? But I suspect something else might be going on. If so, please post your complete unload function.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • Sorry, I should have been clearer. All of that is actually done in load. So there are no temporary nodes in unload. My unload function seems to work fine, but I don't think I am getting rid of those temporary nodes in my load function. When I call free on them, I get segmentation faults. – user10805 Mar 1 '16 at 22:56
  • please edit your question and post all of the unload code. – Cliff B Mar 1 '16 at 23:34
  • So, according to valgrind, you are leaking 105,314,048 bytes in load and not in unload as you initially indicated. Without seeing the related code, it is impossible to tell what's happening. Good luck. – Cliff B Mar 2 '16 at 2:03

You must log in to answer this question.

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