Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC warn for non-freed heap blocks

Tags:

c

gcc

gcc-warning

So the question is simple is there a way to tell the GCC that I want to get warned if I do not free a heap allocated block? I know that we can have non-freed blocks for some purposes/we already reached end of program or something like that.

int main(){
    int *a = malloc(sizeof(int));
    return 0;
}

If I can get a warning even for this it would be awesome.

like image 932
Yakup Türkan Avatar asked Apr 24 '16 11:04

Yakup Türkan


2 Answers

This is not a possible job for GCC to do. Static analysis cannot prove that a free is forgotten, that's the job of run-time analysers like valgrind's memcheck, or eventually gcc -fsanitize=leak, which I haven't seen there yet, only with clang -fsanitize=leak.

But you won't get a compile-time warning, even when gcc or clang supports it. It will be a run-time warning.

like image 145
rurban Avatar answered Oct 30 '22 16:10

rurban


The compiler cannot predict and warn for non-freed blocks. This is runtime job, not compile time. You can implement your own malloc-free-check subsystem or modify memory management library.

like image 37
i486 Avatar answered Oct 30 '22 16:10

i486