Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fprintf memory leak

I am using the Instruments from XCode 4.2.1 to find some memory leaks. I have found a very weird (at least for me) memory leak: (The function values_table_get_value returns a double, and output = stdout) enter image description here

The two questions are: Is it a real memory leak? How can I clean up it? (The fprintf format %.3f is wrong for a double?

To show that the leak is inside the fprintf, I changed the return from the function to 5.0:

enter image description here

and moving the return to a temporary variable:

enter image description here

and to be more precise, here is a picture of the asm code that shows that the leak is: enter image description here

I did a very simple test: printing using the sprintf + fprintf, but I get the leak at sprintf: enter image description here

I also tried to use the printf directly, and I get the leak on it.

I am really thinking that the problem is in format.

The final try, to show that do not have anything related with my function: enter image description here

Just to check, I executed with valgrind: (values_table_print is the function name) valgrind --leak-check=full --show-reachable=yes ./leastsquares

enter image description here

My software versions: valgrind --version: valgrind-3.7.0 gcc --version i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)

like image 388
Pih Avatar asked Jan 28 '12 11:01

Pih


1 Answers

Don't waste time debugging

Your setup probably has a bad gcc or valgrind build or simply isn't up-to-date.

I just tried:

gcc -o junk /tmp/junk.cpp && ~/src/valgrind/coregrind/valgrind --leak-check=full --show-reachable=yes /tmp/junk

on the following snippet

#include <stdio.h>

int main()
{
    printf( "%.3f", 3.0 );
}

Configuration

  • OSX 10.7.2
  • valgrind-3.7.0.SVN
  • gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)

    Using built-in specs. Target: i686-apple-darwin11 Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2335.15~25/src/configure --disable-checking --enable-werror --prefix=/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2335.15~25/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1 Thread model: posix gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)

Valgrind Output

==58980== Memcheck, a memory error detector
==58980== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==58980== Using Valgrind-3.7.0.SVN and LibVEX; rerun with -h for copyright info
==58980== Command: /tmp/junk
==58980== 
--58980-- /tmp/junk:
--58980-- dSYM directory is missing; consider using --dsymutil=yes
UNKNOWN task message [id 3229, to mach_task_self(), reply 0x2503]
UNKNOWN task message [id 3229, to mach_task_self(), reply 0x2503]
UNKNOWN task message [id 3414, to mach_task_self(), reply 0x2503]
--58980-- WARNING: unhandled syscall: unix:357
--58980-- You may be able to write your own handler.
--58980-- Read the file README_MISSING_SYSCALL_OR_IOCTL.
--58980-- Nevertheless we consider this a bug.  Please report
--58980-- it at http://valgrind.org/support/bug_reports.html.
3.000==58980== 
==58980== HEAP SUMMARY:
==58980==     in use at exit: 0 bytes in 0 blocks
==58980==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==58980== 
==58980== All heap blocks were freed -- no leaks are possible
==58980== 
==58980== For counts of detected and suppressed errors, rerun with: -v
==58980== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
like image 160
kfmfe04 Avatar answered Sep 19 '22 07:09

kfmfe04