2

I got a lot of write, read and free errors. I don't know where is mistake. If someone could help me out with this I'd be grateful. Here is log from valgrind:
http://pastebin.com/TR4Ts73Y

void deleteElement(element* node) {
element* child;
if (node == NULL)
    return;

free(node->name);

if (node->text != NULL)
    free(node->text);

child = node->firstChild;
while(child != NULL)
{
    deleteElement(child);
    child = child->nextSibling;
}

free(node);
}

If you need more functions feel free to ask for.

4

1 回答 1

3

The error in the code in the question is here:

while(child != NULL)
{
    deleteElement(child); // this calls free on child
    child = child->nextSibling; // OOPS, child has been freed
}

Here you destroy child and then immediately try to use it again. You need an extra variable.

while(child != NULL)
{
    element* nodeToDelete = child;
    child = child->nextSibling;
    deleteElement(nodeToDelete);
}

There may be other errors in your complete program, but that's the obvious one that can be detected in the code excerpt that you posted.

于 2013-03-30T09:16:31.683 回答