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