Bytes lost -- Forgetting to free

suggest change

Here is a program that calls malloc but not free:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    char *s;

    s = malloc(26); /* The allocation that is never freed. */ 

    return 0;
}

With no extra arguments, valgrind only prints a leak summary, without details about where the leaked block was allocated.

But if we turn on --leak-check=yes (or --leak-check=full), it will complain and display the lines responsible for those memory leaks if the program was compiled in debug mode:

$ valgrind -q --leak-check=yes ./missing_free
==4776== 26 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4776==    at 0x4024F20: malloc (vg_replace_malloc.c:236)
==4776==    by 0x80483F8: main (missing_free.c:8)
==4776==

If the program is not compiled with debug information (for example, without GCC's -g), Valgrind still names the function but not the source line.

This lets us go back and look at what block was allocated in that line and try to trace forward to see why it wasn’t freed.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents