[英]Why won’t valgrind detect a memory leak in a basic program with a leak?
我的代码:
#include <stdlib.h>
#include <stdio.h>
int main() {
char* string = malloc(50 * sizeof(char));
printf("my code ran\n");
return 0;
}
我编译它:
gcc -o mem memErr.c
并运行它
valgrind ./mem
和
valgrind --leak-check=yes ./mem
但我的 output 是
(cmd)(base) ~/Documents/c/summerC$ valgrind --leak-check=yes ./mem
==65172== Memcheck, a memory error detector
==65172== Copyright (C) 2002-2017, and GNU GPL`d, by Julian Seward et al.
==65172== Using Valgrind-3.17.0.GIT-lbmacos and LibVEX; rerun with -h for copyright info
==65172== Command: ./mem
==65172==
==65172== Warning: set address range perms: large range [0x7fff202d0000, 0x8000200d0000) (defined)
==65172== Warning: set address range perms: large range [0x7fff202d0000, 0x7fff7ff88000) (defined)
==65172== Warning: set address range perms: large range [0x7fff8e23c000, 0x7fffc02d0000) (noaccess)
==65172== Warning: set address range perms: large range [0x7fffc02d0000, 0x7fffe2560000) (defined)
==65172== Warning: set address range perms: large range [0x7fffe2560000, 0x7fffffe00000) (noaccess)
my code ran
==65172==
==65172== HEAP SUMMARY:
==65172== in use at exit: 0 bytes in 0 blocks
==65172== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==65172==
==65172== All heap blocks were freed -- no leaks are possible
==65172==
==65172== For lists of detected and suppressed errors, rerun with: -s
==65172== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
为什么 valgrind 不在这里报告 memory 泄漏?
编辑:根据@dbush 和@AJIOB 的评论,我尝试将代码更改为:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
char* string = malloc(50 * sizeof(char));
strcpy(string, "my new code ran");
printf("%s\n", string);
return 0;
}
并编译: gcc -g -o mem memErr.c
但结果是一样的:
(cmd)(base) ~/Documents/c/summerC$ valgrind ./mem
==81073== Memcheck, a memory error detector
==81073== Copyright (C) 2002-2017, and GNU GPL`d, by Julian Seward et al.
==81073== Using Valgrind-3.17.0.GIT-lbmacos and LibVEX; rerun with -h for copyright info
==81073== Command: ./mem
==81073==
==81073== Warning: set address range perms: large range [0x7fff202d0000, 0x8000200d0000) (defined)
==81073== Warning: set address range perms: large range [0x7fff202d0000, 0x7fff7ff88000) (defined)
==81073== Warning: set address range perms: large range [0x7fff8e23c000, 0x7fffc02d0000) (noaccess)
==81073== Warning: set address range perms: large range [0x7fffc02d0000, 0x7fffe2560000) (defined)
==81073== Warning: set address range perms: large range [0x7fffe2560000, 0x7fffffe00000) (noaccess)
my new code ran
==81073==
==81073== HEAP SUMMARY:
==81073== in use at exit: 0 bytes in 0 blocks
==81073== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==81073==
==81073== All heap blocks were freed -- no leaks are possible
==81073==
==81073== For lists of detected and suppressed errors, rerun with: -s
==81073== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
编辑:这个问题似乎是与 valgrind 的 macOS Big Sur 兼容性问题。 我目前的解决方法是使用带有 Docker 的虚拟 linux 环境来运行 valgrind。
由于从未使用过从 malloc 返回的malloc
,因此编译器可能优化了调用。
valgrind output 中的这一行似乎支持了这一点:
total heap usage: 0 allocs, 0 frees, 0 bytes allocated
如果您修改代码以便使用 malloc 的缓冲区,valgrind 应该报告泄漏:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
char* string = malloc(50 * sizeof(char));
strcpy(string, "my code ran");
printf("%s\n", string);
return 0;
}
来自Valgrind 快速入门指南:您应该使用-g
标志(调试符号)编译您的程序。
此外,正如@dbush 用户所说,您应该使用您的字符串来告诉编译器,您的变量确实是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.