2016-12-07 91 views
0

Trynig为libmagic一些测试代码:libmagic中的内存泄漏?

#include <magic.h> 
#include <stdio.h> 

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

    magic_t cookie; 

    cookie = magic_open(MAGIC_MIME_TYPE); 

    if (cookie == NULL) { 
     perror(magic_error(cookie)); 
     return 1; 
    } 

    if (magic_load(cookie, NULL) == -1) { 
     perror(magic_error(cookie)); 
     return 1; 
    } 

    const char *string = magic_file(cookie, *(argv+1)); 

    if (string == NULL) { 
     perror(magic_error(cookie)); 
     return 1; 
    } 

    printf("%s\n", string); 

    magic_close(cookie); 
    return 0; 
} 

的代码几乎从这里撕开:

http://vivithemage.co.uk/blog/?p=105

的代码运行正常,但使用valgrind --leak-check=full ./libmagic /path/to/some/image/file从库中memleak报道:

==6153== HEAP SUMMARY: 
==6153==  in use at exit: 48 bytes in 3 blocks 
==6153== total heap usage: 36 allocs, 33 frees, 990,559 bytes allocated 
==6153== 
==6153== 48 bytes in 1 blocks are definitely lost in loss record 2 of 2 
==6153== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==6153== by 0x4E43D3D: ??? (in /usr/lib/x86_64-linux-gnu/libmagic.so.1.0.0) 
==6153== by 0x4E447ED: ??? (in /usr/lib/x86_64-linux-gnu/libmagic.so.1.0.0) 
==6153== by 0x4008A8: main (libmagic.c:15) 
==6153== 
==6153== LEAK SUMMARY: 
==6153== definitely lost: 48 bytes in 1 blocks 
==6153== indirectly lost: 0 bytes in 2 blocks 
==6153==  possibly lost: 0 bytes in 0 blocks 
==6153== still reachable: 0 bytes in 0 blocks 
==6153==   suppressed: 0 bytes in 0 blocks 

我需要别的东西而不是magic_close()来包装或者在图书馆有一些弱点?

magic.h说MAGIC_VERSION 524,我使用GCC 5.4.0

回答