Question

So I have a php, ruby and python background and got started with C. I'm using Valgrind to check if my programs are not doing silly things but I get this kind of output quite frequently:

14072== <...VALGRIND HEADER & COPYRIGHT...>
14158== HEAP SUMMARY:
14158==     in use at exit: 137,084 bytes in 196 blocks
14158==   total heap usage: 247 allocs, 51 frees, 149,496 bytes allocated
14158== 
14158== 7 bytes in 1 blocks are definitely lost in loss record 3 of 74 at
14158==    0x4C2745D: malloc in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
14158==    by 0x50F3369: strdup in /usr/lib64/libc-2.18.so
14158==    by 0x4E51B34: readline in /usr/lib64/libedit.so.0.0.43
14158==    by 0x40083C: main in /home/<program location>/
14158== 
14158== LEAK SUMMARY:
14158==    definitely lost: 7 bytes in 1 blocks
14158==    indirectly lost: 0 bytes in 0 blocks
14158==      possibly lost: 0 bytes in 0 blocks
14158==    still reachable: 137,077 bytes in 195 blocks
14158==         suppressed: 0 bytes in 0 blocks
14158== Reachable blocks (those to which a pointer was found) are not shown.
14158== To see them, rerun with: --leak-check=full --show-leak-kinds=all
14158== 
14158== For counts of detected and suppressed errors, rerun with: -v
14158== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

This valgrind debug output is from this quick REPL that simply shouts back user input on the screen or quits in case input equals to :exit:

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

#include <editline/readline.h>

static char prompt[5]   = "repl>";
static char* cmd_exit = ":exit";

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

    puts("Press Ctrl+c to Exit\n");

    while(1) {
        char* input = readline(prompt);
        add_history(input);
        if(strcmp(input,cmd_exit) == 0) {
            puts("Bye!");
            return 0;
        }
        printf("%s\n", input);
        free(input);
    }
    return 0;
}

I've already tried to free both prompt and cmd_exit variables before function main returns a value but leak is still there according to Valgrind.

...
    free(prompt);
    free(cmd_exit);
    return 0;
}

Some questions:

  • How to get rid of this reported memory leak?
  • Should I be using static variables in this situation?
  • Should I free those static variables too?

Any good advice or material is welcome.

Was it helpful?

Solution

You only need to free things you malloced, so you don't need to free the static variables.

As your readline function seems to use strdup and that uses malloc/calloc, you need to free input. You do this in the loop which is good, but you missed it when you exit the loop:

if(strcmp(input,cmd_exit) == 0) {
        puts("Bye!");
        free(input); 
        return 0;
    }

By the way:

static char prompt[6]   = "repl>";

The text "repl>" has 6 chars, r, e, p, l, > and a nul byte to mark the end of the string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top