我在文件研制出纯C语言实现FIFO列表(队列)的fifo.h和fifo.c,并写了一个测试程序testfifo.c,我编译成./bin/testfifo.节点结构定义于list.h.
我在OS X 10.6上通过Valgrind运行我的程序
valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./bin/testfifo
Run Code Online (Sandbox Code Playgroud)
并获得以下输出
==54688== Memcheck, a memory error detector
==54688== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==54688== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==54688== Command: bin/testfifo
==54688==
--54688-- bin/testfifo:
--54688-- dSYM directory is missing; consider using --dsymutil=yes
==54688==
==54688== HEAP SUMMARY:
==54688== in use at exit: 88 bytes in 1 blocks
==54688== total heap …Run Code Online (Sandbox Code Playgroud) 今天我在我的Mac os x 10.6上安装了valgrind并尝试测试它.事实证明,系统中存在奇怪的内存泄漏.我所做的只是创建简单的c文件,获取一些堆内存并立即释放它.当我跑valgrind时,它表现出类似的东西
Realfrees-MacBook-Pro:C Realfree$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out
==2621== Memcheck, a memory error detector
==2621== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2621== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==2621== Command: ./a.out
==2621==
--2621-- ./a.out:
--2621-- dSYM directory is missing; consider using --dsymutil=yes
==2621==
==2621== HEAP SUMMARY:
==2621== in use at exit: 88 bytes in 1 blocks
==2621== total heap usage: 2 allocs, 1 frees, 92 bytes allocated
==2621== …Run Code Online (Sandbox Code Playgroud) 我在valgrind中遇到"额外"内存泄漏问题.例如,我创建了一个名为temp.cpp的测试程序:
int main() { return 0; }
Run Code Online (Sandbox Code Playgroud)
在终端,我运行:
>> g++ -o temp.out temp.cpp
>> valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all ./temp.out
Run Code Online (Sandbox Code Playgroud)
这会导致几次内存泄漏.怎么会这样?
==4438== LEAK SUMMARY:
==4438== definitely lost: 4,120 bytes in 2 blocks
==4438== indirectly lost: 2,288 bytes in 6 blocks
==4438== possibly lost: 8,336 bytes in 99 blocks
==4438== still reachable: 6,440 bytes in 13 blocks
==4438== suppressed: 5,020 bytes in 73 blocks
Run Code Online (Sandbox Code Playgroud)
我试过运行其他.cpp文件,我得到完全相同的泄漏摘要.大约一个月前,当我尝试时,没有错.我可能已经升级了Xcode或其他东西,如果这可能是问题(?).这些是我对g ++的设置:
配置为:
--prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1
Run Code Online (Sandbox Code Playgroud)
Apple LLVM版本6.1.0(clang-602.0.53)(基于LLVM 3.6.0svn)目标:x86_64-apple-darwin15.0.0线程模型:posix
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
char *str = malloc(sizeof(char)*5);
str = strcpy(str, "test");
printf("%s\n", str);
free(str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我在Mac上使用Valgrind(OS X,10.9.5)时,我收到以下消息:
==77215== HEAP SUMMARY:
==77215== in use at exit: 29,211 bytes in 374 blocks
==77215== total heap usage: 451 allocs, 77 frees, 35,160 bytes allocated
==77215==
==77215== 4,096 bytes in 1 blocks are still reachable in loss record 76 of 76
==77215== at 0x66CB: malloc (in /usr/local/Cellar/valgrind/3.10.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==77215== by 0x182855: __smakebuf (in /usr/lib/system/libsystem_c.dylib)
==77215== …Run Code Online (Sandbox Code Playgroud) 我得到一些来自的valgrind错误std::.所以我将我的程序条带化为非常简单的"Hello World"示例:
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){
cout << "Hello World!" << endl; // (Line 8)
}
Run Code Online (Sandbox Code Playgroud)
但我仍然收到以下错误:
==1311== Conditional jump or move depends on uninitialised value(s)
==1311== at 0x1004E8C3F: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==1311== by 0x1002DCB96: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==1311== by 0x1002DD0AA: fwrite (in /usr/lib/system/libsystem_c.dylib)
==1311== by 0x100029D29: std::__1::__stdoutbuf<char>::overflow(int) (in /usr/lib/libc++.1.dylib)
==1311== by 0x10001F91C: std::__1::basic_streambuf<char, std::__1::char_traits<char> >::xsputn(char const*, long) (in /usr/lib/libc++.1.dylib)
==1311== by 0x100001C88: std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char …Run Code Online (Sandbox Code Playgroud) 我正在学习C艰难的方式,我正在练习4:介绍Valgrind.我在Mac OS X Yosemite上,在撰写本文时,没有稳定的Valgrind for Yosemite版本.我找到了Yosemite和Valgrind,并使用了最高投票答案的指示brew install --HEAD valgrind.这个安装了Valgrind和我能够跟随Zed的练习.但是,当我"修复"应用程序时,我仍然遇到错误.
为了仔细检查,我回到练习3,这应该没有错误,但我仍然在Valgrind中出错.这是代码,然后是输出:
ex3.c
#include <stdio.h>
int main()
{
int age = 10;
int height = 72;
printf("I am %d years old.\n", age);
printf("I am %d inches tall.\n", height);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在iTerm中:
ransom:learn-c-the-hard-way ben$ rm -f ex3
ransom:learn-c-the-hard-way ben$ make ex3
cc -Wall -g ex3.c -o ex3
ransom:learn-c-the-hard-way ben$ valgrind ./ex3
==8795== Memcheck, a memory error detector
==8795== Copyright (C) 2002-2013, and …Run Code Online (Sandbox Code Playgroud)