Valgrind Memory leaks when parsing through an XML file

Viewed 26

So currently I am writing a XMLParser into a Tree and its working fine but the problem is its using too much Memmory and when I am trying to run it with a 4GB XML file it is not working due to lack of memmory.

So what im doing is going through first converting the XML File into a char* and then Parsing throught it and using malloc to allocate memmory for each Node structure.

When exactly am I supposed to Free() these nodes ? Am I even supposed to do that ? I just dont understand when exactly am I supposed to free stuff basically. I am getting Valgrind errors

==3002== HEAP SUMMARY:
==3002==     in use at exit: 8,995 bytes in 423 blocks
==3002==   total heap usage: 427 allocs, 137 frees, 24,691 bytes allocated
==3002== 
==3002== 19 bytes in 1 blocks are indirectly lost in loss record 1 of 8
==3002==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==3002==    by 0x491460E: strdup (strdup.c:42)
==3002==    by 0x109B9D: parse (main.c:223)
==3002==    by 0x10A06B: main (main.c:333)
==3002== 
==3002== 48 bytes in 1 blocks are indirectly lost in loss record 2 of 8
==3002==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==3002==    by 0x10934B: new_node (main.c:9)
==3002==    by 0x10A054: main (main.c:332)
==3002== 
==3002== 206 bytes in 32 blocks are definitely lost in loss record 3 of 8
==3002==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==3002==    by 0x491460E: strdup (strdup.c:42)
==3002==    by 0x1096E9: parse (main.c:108)
==3002==    by 0x10A06B: main (main.c:333)
==3002== 
==3002== 551 bytes in 101 blocks are indirectly lost in loss record 4 of 8
==3002==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==3002==    by 0x491460E: strdup (strdup.c:42)
==3002==    by 0x1096E9: parse (main.c:108)
==3002==    by 0x10A06B: main (main.c:333)
==3002== 
==3002== 608 bytes in 19 blocks are indirectly lost in loss record 5 of 8
==3002==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==3002==    by 0x109648: add_attribute (main.c:85)
==3002==    by 0x109DC4: parse (main.c:275)
==3002==    by 0x10A06B: main (main.c:333)
==3002== 
==3002== 1,083 bytes in 134 blocks are indirectly lost in loss record 6 of 8
==3002==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==3002==    by 0x491460E: strdup (strdup.c:42)
==3002==    by 0x109D13: parse (main.c:255)
==3002==    by 0x10A06B: main (main.c:333)
==3002== 
==3002== 6,432 bytes in 134 blocks are indirectly lost in loss record 7 of 8
==3002==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==3002==    by 0x10934B: new_node (main.c:9)
==3002==    by 0x109C3A: parse (main.c:238)
==3002==    by 0x10A06B: main (main.c:333)
==3002== 
==3002== 8,789 (48 direct, 8,741 indirect) bytes in 1 blocks are definitely lost in loss record 8 of 8
==3002==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==3002==    by 0x10934B: new_node (main.c:9)
==3002==    by 0x109B7E: parse (main.c:221)
==3002==    by 0x10A06B: main (main.c:333)
==3002== 
==3002== LEAK SUMMARY:
==3002==    definitely lost: 254 bytes in 33 blocks
==3002==    indirectly lost: 8,741 bytes in 390 blocks
==3002==      possibly lost: 0 bytes in 0 blocks
==3002==    still reachable: 0 bytes in 0 blocks
==3002==         suppressed: 0 bytes in 0 blocks
==3002== 
==3002== For lists of detected and suppressed errors, rerun with: -s

My thought process was that If I free these nodes then the Tree is destroyed in the proccess. Or is it because I am allocating EXTRA stuff that is not needed? TY

1 Answers

When exactly am I supposed to Free() these nodes ? Am I even supposed to do that ? I just dont understand when exactly am I supposed to free stuff basically. I am getting Valgrind errors

After using either malloc(), calloc() or realloc(), a pointer will be returned to you to some memory on the heap (assuming no error occurred).

You should free() the memory using the pointer returned from these functions once you no longer need the memory that was allocated.

Example:

struct node* new_node = malloc(sizeof(struct node));

if (new_node == NULL) {
    // malloc() failed 
}

// do stuff with new_node

free(new_node); // no longer need new_node
Related