Leak detection using valgrind when using fopen

gcc 4.4.4 c89 valgrind 3.5.0.

I get a leak detected when opening a file with a file pointer.

==17681== in use at exit: 352 bytes in 1 blocks ==17681== total heap usage: 1 allocs, 0 frees, 352 bytes allocated ==17681== ==17681== 352 bytes in 1 blocks are still reachable in loss record 1 of 1 ==17681== at 0x4005BDC: malloc (vg_replace_malloc.c:195) ==17681== by 0xAAD67E: __fopen_internal (iofopen.c:76) ==17681== by 0xAAD74B: fopen@ @GLIBC_2.1 (iofopen.c:107) 

The line of code he points to is fopen:

 FILE *fp = NULL; fp = fopen("input.txt", "r"); if(fp == NULL) { fprintf(stderr, "Failed to open file [ %s ]\n", strerror(errno)); exit(1); } 

Maybe the fopen function allocates memory and does not free it? How can I free this memory?

Many thanks for any suggestions,

+4
source share
2 answers

You have not closed your FILE *.

+6
source

I would suggest that he release it if you call fclose in the file.

+1
source

All Articles