开发者

leak detected using valgrind when using fopen

开发者 https://www.devze.com 2023-01-14 02:29 出处:网络
gcc 4.4.4 c89 valgrind 3.5.0. I am getting a leak detected when I open file using a file pointer. ==17681==in use at exit: 352 bytes in 1 bl开发者_Go百科ocks

gcc 4.4.4 c89 valgrind 3.5.0.

I am getting a leak detected when I open file using a file pointer.

==17681==    in use at exit: 352 bytes in 1 bl开发者_Go百科ocks
==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 code line it is pointing to is this fopen:

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

Could it be that the fopen function is allocating memory and not freeing it? How can I free this memory?

Many thank for any suggestions,


You didn't fclose your FILE*.


I had the same problem

I solved it with a fflush()

// Stream needs to be flushed before closing
fflush ( fp);

fclose( fp);


I'd guess it'll free it if you call fclose on the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消