Valgrind 报告一个非常简单的 C 程序的错误
Valgrind reports errors for a very simple C program
我正在从 Learn C The Hard Way. I'm on exercise 6 学习 C 语言,虽然我可以让它工作,但 valgrind 报告了很多错误。
这是文件 ex6.c
中精简的最小程序:
#include <stdio.h>
int main(int argc, char *argv[])
{
char initial = 'A';
float power = 2.345f;
printf("Character is %c.\n", initial);
printf("You have %f levels of power.\n", power);
return 0;
}
Makefile
的内容只是 CFLAGS=-Wall -g
。
我用 $ make ex6
编译程序(没有编译器警告或错误)。使用 $ ./ex6
执行会产生预期的输出。
当我使用 $ valgrind ./ex6
运行程序时,出现无法解决的错误。这是完整的输出:
==69691== Memcheck, a memory error detector
==69691== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==69691== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==69691== Command: ./ex6
==69691==
--69691-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--69691-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--69691-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
==69691== Conditional jump or move depends on uninitialised value(s)
==69691== at 0x1003FBC3F: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==69691== by 0x1001EFBB6: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001FA005: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x10021F9CE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x10021FCA0: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001F5B91: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001F39F7: printf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x100000F1B: main (ex6.c:8)
==69691==
Character is A.
==69691== Invalid read of size 32
==69691== at 0x1003FBC1D: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==69691== by 0x1001EFBB6: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001FA005: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x10021F9CE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x10021FCA0: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001F5B91: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001F39F7: printf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x100000F31: main (ex6.c:9)
==69691== Address 0x100809680 is 32 bytes before a block of size 32 in arena "client"
==69691==
You have 2.345000 levels of power.
==69691==
==69691== HEAP SUMMARY:
==69691== in use at exit: 39,365 bytes in 429 blocks
==69691== total heap usage: 510 allocs, 81 frees, 45,509 bytes allocated
==69691==
==69691== LEAK SUMMARY:
==69691== definitely lost: 16 bytes in 1 blocks
==69691== indirectly lost: 0 bytes in 0 blocks
==69691== possibly lost: 13,090 bytes in 117 blocks
==69691== still reachable: 26,259 bytes in 311 blocks
==69691== suppressed: 0 bytes in 0 blocks
==69691== Rerun with --leak-check=full to see details of leaked memory
==69691==
==69691== For counts of detected and suppressed errors, rerun with: -v
==69691== Use --track-origins=yes to see where uninitialised values come from
==69691== ERROR SUMMARY: 5 errors from 2 contexts (suppressed: 0 from 0)
我在 OS X yosemite。使用此命令 $ brew install valgrind --HEAD
.
通过 brew
安装 Valgrind
那么,有人知道这里的问题是什么吗?如何修复 valgrind 错误?
如果您通过 Valgrind 运行 使用的程序正是您在问题中发布的程序,那么它显然没有任何内存泄漏。事实上,您甚至不会自己使用 malloc/free!
在我看来,这些是 Valgrind 在 OS X(仅!)上检测到的虚假错误/误报,类似于 what happened to myself some time ago。
如果您可以访问不同的操作系统,例如Linux 机器,尝试在该系统上使用 Valgrind 分析程序。
编辑: 我自己还没有尝试过,因为我现在无法访问 Mac,但你应该尝试一下
M Oehm 建议:try to use a supressions file as mentioned in this other SO question.
从 this 主题来看,我假设不能保证 valgrind 在您的平台上给出正确的结果。如果可以,请在其他平台上尝试此代码。
罪魁祸首要么在 valgrid 本身,要么在您系统的 printf
实现中,这两者对您来说都是不切实际的。
Rerun with --leak-check=full to see details of leaked memory.
这应该会为您提供有关您遇到的泄漏的更多信息。如果没有任何帮助,您可以创建一个 suppression file 来阻止显示错误。
此问题已针对 Darwin 14.3.0 (Mac OS X 10.10.2) 使用 Valgrind r14960 with VEX r3124
for Xcode6.2 和 Valgrind r15088
修复对于 Xcode 6.3.
如果您正在使用 Mac 端口(在撰写本文时),sudo port install valgrind-devel
将为您提供 Valgrind r14960 with VEX r3093
。
这是我要安装的构建脚本 Valgrind r14960 with VEX r3124
:
#! /usr/bin/env bash
mkdir -p buildvalgrind
cd buildvalgrind
svn co svn://svn.valgrind.org/valgrind/trunk/@14960 valgrind
cd valgrind
./autogen.sh
./configure --prefix=/usr/local
make && sudo make install
# check that we have our valgrind installed
/usr/local/bin/valgrind --version
(参考:http://calvinx.com/2015/04/10/valgrind-on-mac-os-x-10-10-yosemite/)
我的 macports 安装的 valgrind 位于 /opt/local/bin/valgrind
。
如果我现在运行
/opt/local/bin/valgrind --leak-check=yes --suppressions=`pwd`/objc.supp ./ex6
我会得到与您上面描述的完全相同的错误。 (在这里使用我的 objc.supp
文件 https://gist.github.com/calvinchengx/0b1d45f67be9fdca205b)
但是如果我运行
/usr/local/bin/valgrind --leak-check=yes --suppressions=`pwd`/objc.supp ./ex6
一切都按预期工作,我没有出现系统级内存泄漏错误。
我正在从 Learn C The Hard Way. I'm on exercise 6 学习 C 语言,虽然我可以让它工作,但 valgrind 报告了很多错误。
这是文件 ex6.c
中精简的最小程序:
#include <stdio.h>
int main(int argc, char *argv[])
{
char initial = 'A';
float power = 2.345f;
printf("Character is %c.\n", initial);
printf("You have %f levels of power.\n", power);
return 0;
}
Makefile
的内容只是 CFLAGS=-Wall -g
。
我用 $ make ex6
编译程序(没有编译器警告或错误)。使用 $ ./ex6
执行会产生预期的输出。
当我使用 $ valgrind ./ex6
运行程序时,出现无法解决的错误。这是完整的输出:
==69691== Memcheck, a memory error detector
==69691== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==69691== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==69691== Command: ./ex6
==69691==
--69691-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--69691-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--69691-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
==69691== Conditional jump or move depends on uninitialised value(s)
==69691== at 0x1003FBC3F: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==69691== by 0x1001EFBB6: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001FA005: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x10021F9CE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x10021FCA0: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001F5B91: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001F39F7: printf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x100000F1B: main (ex6.c:8)
==69691==
Character is A.
==69691== Invalid read of size 32
==69691== at 0x1003FBC1D: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==69691== by 0x1001EFBB6: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001FA005: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x10021F9CE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x10021FCA0: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001F5B91: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x1001F39F7: printf (in /usr/lib/system/libsystem_c.dylib)
==69691== by 0x100000F31: main (ex6.c:9)
==69691== Address 0x100809680 is 32 bytes before a block of size 32 in arena "client"
==69691==
You have 2.345000 levels of power.
==69691==
==69691== HEAP SUMMARY:
==69691== in use at exit: 39,365 bytes in 429 blocks
==69691== total heap usage: 510 allocs, 81 frees, 45,509 bytes allocated
==69691==
==69691== LEAK SUMMARY:
==69691== definitely lost: 16 bytes in 1 blocks
==69691== indirectly lost: 0 bytes in 0 blocks
==69691== possibly lost: 13,090 bytes in 117 blocks
==69691== still reachable: 26,259 bytes in 311 blocks
==69691== suppressed: 0 bytes in 0 blocks
==69691== Rerun with --leak-check=full to see details of leaked memory
==69691==
==69691== For counts of detected and suppressed errors, rerun with: -v
==69691== Use --track-origins=yes to see where uninitialised values come from
==69691== ERROR SUMMARY: 5 errors from 2 contexts (suppressed: 0 from 0)
我在 OS X yosemite。使用此命令 $ brew install valgrind --HEAD
.
brew
安装 Valgrind
那么,有人知道这里的问题是什么吗?如何修复 valgrind 错误?
如果您通过 Valgrind 运行 使用的程序正是您在问题中发布的程序,那么它显然没有任何内存泄漏。事实上,您甚至不会自己使用 malloc/free!
在我看来,这些是 Valgrind 在 OS X(仅!)上检测到的虚假错误/误报,类似于 what happened to myself some time ago。
如果您可以访问不同的操作系统,例如Linux 机器,尝试在该系统上使用 Valgrind 分析程序。
编辑: 我自己还没有尝试过,因为我现在无法访问 Mac,但你应该尝试一下 M Oehm 建议:try to use a supressions file as mentioned in this other SO question.
从 this 主题来看,我假设不能保证 valgrind 在您的平台上给出正确的结果。如果可以,请在其他平台上尝试此代码。
罪魁祸首要么在 valgrid 本身,要么在您系统的 printf
实现中,这两者对您来说都是不切实际的。
Rerun with --leak-check=full to see details of leaked memory.
这应该会为您提供有关您遇到的泄漏的更多信息。如果没有任何帮助,您可以创建一个 suppression file 来阻止显示错误。
此问题已针对 Darwin 14.3.0 (Mac OS X 10.10.2) 使用 Valgrind r14960 with VEX r3124
for Xcode6.2 和 Valgrind r15088
修复对于 Xcode 6.3.
如果您正在使用 Mac 端口(在撰写本文时),sudo port install valgrind-devel
将为您提供 Valgrind r14960 with VEX r3093
。
这是我要安装的构建脚本 Valgrind r14960 with VEX r3124
:
#! /usr/bin/env bash
mkdir -p buildvalgrind
cd buildvalgrind
svn co svn://svn.valgrind.org/valgrind/trunk/@14960 valgrind
cd valgrind
./autogen.sh
./configure --prefix=/usr/local
make && sudo make install
# check that we have our valgrind installed
/usr/local/bin/valgrind --version
(参考:http://calvinx.com/2015/04/10/valgrind-on-mac-os-x-10-10-yosemite/)
我的 macports 安装的 valgrind 位于 /opt/local/bin/valgrind
。
如果我现在运行
/opt/local/bin/valgrind --leak-check=yes --suppressions=`pwd`/objc.supp ./ex6
我会得到与您上面描述的完全相同的错误。 (在这里使用我的 objc.supp
文件 https://gist.github.com/calvinchengx/0b1d45f67be9fdca205b)
但是如果我运行
/usr/local/bin/valgrind --leak-check=yes --suppressions=`pwd`/objc.supp ./ex6
一切都按预期工作,我没有出现系统级内存泄漏错误。