Estou aprendendo a linguagem C do Learn C The Hard Way . Eu estou no exercício 6 e enquanto eu posso fazê-lo funcionar, valgrind repete muitos erros.
Aqui está o programa mínimo despojado de um arquivo ex6.c
:
#include 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; }
O conteúdo do Makefile
é apenas CFLAGS=-Wall -g
.
Eu compilo o programa com $ make ex6
(não há avisos ou erros do compilador). Executar com $ ./ex6
produz a saída esperada.
Quando executo o programa com $ valgrind ./ex6
, recebo erros que não consigo resolver. Aqui está a saída completa:
==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)
Eu estou no yosemite do OS X. Valgrind é instalado via brew
com este comando $ brew install valgrind --HEAD
.
Então, alguém sabe qual é o problema aqui? Como corrijo os erros valgrind?
Se o programa que você está executando através do Valgrind é exatamente o que você postou na sua pergunta, ele claramente não tem nenhum memory leaks. Na verdade, você nem usa malloc / free yourself!
Parece-me que estes são falsos erros / falsos positivos que Valgrind detecta no OS X (apenas!), Semelhante ao que aconteceu comigo há algum tempo atrás .
Se você tiver access a um sistema operacional diferente, por exemplo, uma máquina Linux, tente analisar o programa usando Valgrind nesse sistema.
EDIT: Eu não tentei isso sozinho, desde que eu não tenho access a um Mac agora, mas você deve tentar o que M Oehm sugeriu: tente usar um arquivo de supressões como mencionado nesta outra pergunta SO .
Este problema foi corrigido para o Darwin 14.3.0 (Mac OS X 10.10.2) usando o Valgrind r14960 with VEX r3124
para o Xcode6.2 e o Valgrind r15088
para o Xcode 6.3.
Se você estiver usando Macports (neste momento), o sudo port install valgrind-devel
fornecerá o Valgrind r14960 with VEX r3093
.
Aqui está o meu script de construção para instalar o 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
(referência: http://calvinx.com/2015/04/10/valgrind-on-mac-os-x-10-10-yosemite/ )
Meu valgrind instalado pelo macports está localizado em /opt/local/bin/valgrind
.
Se eu agora corro
/opt/local/bin/valgrind --leak-check=yes --suppressions=`pwd`/objc.supp ./ex6
Vou obter exatamente os mesmos erros que você descreveu acima. (Usando meu arquivo objc.supp
aqui https://gist.github.com/calvinchengx/0b1d45f67be9fdca205b )
Mas se eu correr
/usr/local/bin/valgrind --leak-check=yes --suppressions=`pwd`/objc.supp ./ex6
Tudo funciona como esperado e não obtenho erros de memory leaks no nível do sistema aparecendo.
A julgar por este tópico, presumo que não é garantido que o valgrind dê resultados corretos em sua plataforma. Se você puder, tente este código em outra plataforma.
O culpado está em valgrid ou na implementação do printf
do seu sistema, e ambos seriam impraticáveis para você corrigir.
Rerun with --leak-check=full to see details of leaked memory.
Isso deve lhe dar mais algumas informações sobre o vazamento que você está enfrentando. Se nada ajudar, você pode criar um arquivo de supressão para impedir que os erros sejam exibidos.