Valgrind reports errors for a very simple C program

假装没事ソ 提交于 2019-11-26 17:55:51
Genba

If the programme you are running through Valgrind is exactly the one you posted in your question, it clearly doesn't have any memory leaks. In fact, you don't even use malloc/free yourself!

It looks to me like these are spurious errors / false positives that Valgrind detects on OS X (only!), similar to what happened to myself some time ago.

If you have access to a different operating system, e.g. a Linux machine, try to analyze the programme using Valgrind on that system.

EDIT: I haven't tried this myself, since I don't have access to a Mac right now, but you should try what M Oehm suggested: try to use a supressions file as mentioned in this other SO question.

This issue is fixed for Darwin 14.3.0 (Mac OS X 10.10.2) using Valgrind r14960 with VEX r3124 for Xcode6.2 and Valgrind r15088 for Xcode 6.3.

If you are using Macports (at this time of writing), sudo port install valgrind-devel will give you Valgrind r14960 with VEX r3093.

Here's my build script to install 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 

(reference: http://calvinx.com/2015/04/10/valgrind-on-mac-os-x-10-10-yosemite/)

My macports-installed valgrind is located at /opt/local/bin/valgrind.

If I now run

/opt/local/bin/valgrind --leak-check=yes --suppressions=`pwd`/objc.supp ./ex6

I will get exactly the same errors you described above. (Using my objc.supp file here https://gist.github.com/calvinchengx/0b1d45f67be9fdca205b)

But if I run

/usr/local/bin/valgrind --leak-check=yes --suppressions=`pwd`/objc.supp ./ex6

Everything works as expected and I do not get the system level memory leak errors showing up.

Daerst

Judging from this topic, I assume that valgrind is not guaranteed to give correct results on your platform. If you can, try this code on another platform.

The culprit is either in valgrid itself or in your system's implementation of printf, both of which would be impractical for you to fix.

Rerun with --leak-check=full to see details of leaked memory. This should give you some more information about the leak you are experiencing. If nothing helps, you can create a suppression file to stop the errors from being displayed.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!