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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490

1 Answers1

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks! Is it somehow possible to send you the rest of the code? I can't share it publicly so... –  Mar 30 '13 at 09:26
  • I've put there other functions –  Mar 30 '13 at 10:26
  • If you can't post them here, then we can't help. I rolled back the question. I think I answered the question that you asked. If you have further problems, make that further questions. – David Heffernan Mar 30 '13 at 10:36