簡體   English   中英

Valgrind報告一個非常簡單的C程序的錯誤

[英]Valgrind reports errors for a very simple C program

我正在通過“學習C 艱辛的方式”學習C語言。 我正在練習6 ,雖然我可以使它起作用,但是valgrind報告很多錯誤。

這是ex6.c文件中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優勝美地。 Valgrind是通過安裝brew用這個命令$ brew install valgrind --HEAD

那么,有人知道這是什么問題嗎? 如何解決valgrind錯誤?

如果您通過Valgrind運行的程序恰好是您在問題中發布的程序,則顯然沒有任何內存泄漏。 實際上,您自己甚至都不使用malloc / free!

在我看來,這些都是Valgrind在OS X上(僅!)檢測到的虛假錯誤/誤報,類似於一段時間前發生在我身上的情況

如果您可以訪問其他操作系統,例如Linux機器,請嘗試在該系統上使用Valgrind分析程序。

編輯:我自己還沒有嘗試過此操作,因為我現在無法訪問Mac,但是您應該嘗試M Oehm的建議: 嘗試使用另一個SO問題中提到的 supressions 文件

對於使用Valgrind r14960 with VEX r3124 r3124和Xcode 6.3的Valgrind r15088 Darwin 14.3.0(Mac OS X 10.10.2),此問題已修復。

如果您正在使用Macports(在撰寫本文時),那么sudo port install valgrind-devel將為您Valgrind r14960 with VEX r3093

這是我Valgrind r14960 with VEX r3124安裝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

一切都按預期工作,並且我沒有出現系統級內存泄漏錯誤。

主題來看,我認為不能保證valgrind在您的平台上給出正確的結果。 如果可以,請在其他平台上嘗試此代碼。

罪魁禍首要么在valgrid本身,要么在系統的printf實現中,而這兩者對於您都無法修復。

Rerun with --leak-check=full to see details of leaked memory. 這應該為您提供有關所遇到的泄漏的更多信息。 如果沒有幫助,則可以創建一個抑制文件以阻止顯示錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM