fprintf内存泄漏

时间:2012-01-28 11:21:30

标签: c xcode memory-leaks instruments valgrind

我正在使用XCode 4.2.1中的Instruments来查找一些内存泄漏。 我发现了一个非常奇怪的(至少对我而言)内存泄漏: (函数values_table_get_value返回一个double,output = stdoutenter image description here

这两个问题是: 这是真正的内存泄漏吗? 我该如何清理它? (fprintf

%.3f格式double错误

为了显示泄漏是在fprintf中,我将函数的返回值更改为5.0

enter image description here

并将返回值移至临时变量:

enter image description here

更准确地说,这是一张asm代码图片,显示泄漏是: enter image description here

我做了一个非常简单的测试:使用sprintf + fprintf打印,但是我在sprintf上得到了泄漏: enter image description here

我也尝试直接使用printf,我就会漏掉它。

我真的认为问题是格式化的。

最后一次尝试,表明我的功能没有任何相关内容: enter image description here

只是为了检查,我用valgrind执行:(values_table_print是函数名) valgrind --leak-check=full --show-reachable=yes ./leastsquares

enter image description here

我的软件版本: valgrind --version:valgrind-3.7.0 gcc --version i686-apple-darwin11-llvm-gcc-4.2(GCC)4.2.1(基于Apple Inc. build 5658)(LLVM build 2336.1.00)

1 个答案:

答案 0 :(得分:5)

不要浪费时间调试

您的设置可能包含错误的gcc或valgrind版本,或者根本不是最新的。

我刚试过:

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

在以下代码段

#include <stdio.h>

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

配置

  • OSX 10.7.2
  • 的valgrind-3.7.0.SVN
  • gcc 4.2.1版(基于Apple Inc. build 5658)(LLVM build 2335.15.00)

    使用内置规格。 目标:i686-apple-darwin11 配置为:/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 线程模型:posix gcc版本4.2.1(基于Apple Inc. build 5658)(LLVM build 2335.15.00)

Valgrind输出

==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)
相关问题