Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C fprintf内存泄漏_C_Xcode_Memory Leaks_Instruments_Valgrind - Fatal编程技术网

C fprintf内存泄漏

C fprintf内存泄漏,c,xcode,memory-leaks,instruments,valgrind,C,Xcode,Memory Leaks,Instruments,Valgrind,我正在使用XCode 4.2.1中的工具查找一些内存泄漏。 我发现了一个非常奇怪的(至少对我来说)内存泄漏: (函数values\u table\u get\u value返回一个double,并且output=stdout) 这两个问题是: 这真的是内存泄漏吗? 我该怎么清理呢?(fprintf格式%.3f对于双精度是错误的 为了显示泄漏在fprintf内部,我将函数返回值更改为5.0: 以及将返回移动到临时变量: 更准确地说,下面是asm代码的图片,显示泄漏是: 我做了一个非常简单的

我正在使用XCode 4.2.1中的工具查找一些内存泄漏。 我发现了一个非常奇怪的(至少对我来说)内存泄漏: (函数
values\u table\u get\u value
返回一个double,并且
output=stdout

这两个问题是: 这真的是内存泄漏吗? 我该怎么清理呢?(fprintf格式
%.3f
对于双精度
是错误的

为了显示泄漏在fprintf内部,我将函数返回值更改为
5.0

以及将返回移动到临时变量:

更准确地说,下面是asm代码的图片,显示泄漏是:

我做了一个非常简单的测试:使用
sprintf
+
fprintf
打印,但是我在sprintf中发现了漏洞:

我还试着直接使用
printf
,结果发现了漏洞

我真的认为问题出在形式上。

最后一次尝试,表明与我的功能无关:

为了检查,我使用valgrind执行:(
values\u table\u print
是函数名)
valgrind--leak check=full--show reachable=yes./leastsquares

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

不要浪费时间调试 您的安装程序可能具有错误的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 );
}

抽象它,这个函数返回一个double,泄漏在fprintf中。这里的重要问题是:输出的定义是什么?在这种情况下,output=stdout。因此,我猜第一次使用stdout时会创建一个缓冲区。这会明显泄漏,但并不重要,因为它无论如何都会在程序退出时关闭。要验证这一点,请可以尝试用fputc(32,输出)替换fprintf;并检查在处理该特定格式时,相同的伪内存泄漏是否可能是C库中的内存泄漏。感谢您的回答。事实上,我使用的是与您相同的valgrind、gcc和max os x版本。但我会接受您的回答,因为您做了一个很好的测试。
==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)