Valgrind报告了snprintf内存泄漏

时间:2016-07-16 22:44:25

标签: c memory-leaks printf valgrind

这个简单的程序

==29114== Memcheck, a memory error detector
==29114== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==29114== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==29114== Command: ./test
==29114==
==29114==
==29114== HEAP SUMMARY:
==29114==     in use at exit: 22,175 bytes in 187 blocks
==29114==   total heap usage: 263 allocs, 76 frees, 28,271 bytes allocated
==29114==
==29114== 148 (80 direct, 68 indirect) bytes in 1 blocks are definitely lost in loss record 43 of 66
==29114==    at 0x100008EBB: malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==29114==    by 0x1001C34A2: __Balloc_D2A (in /usr/lib/system/libsystem_c.dylib)
==29114==    by 0x1001C3DEB: __d2b_D2A (in /usr/lib/system/libsystem_c.dylib)
==29114==    by 0x1001C0443: __dtoa (in /usr/lib/system/libsystem_c.dylib)
==29114==    by 0x1001E907A: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==29114==    by 0x10021235C: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==29114==    by 0x1001F65A8: _vsnprintf (in /usr/lib/system/libsystem_c.dylib)
==29114==    by 0x1001F665D: vsnprintf (in /usr/lib/system/libsystem_c.dylib)
==29114==    by 0x100227CBF: __snprintf_chk (in /usr/lib/system/libsystem_c.dylib)
==29114==    by 0x100000F4B: main (in ./test)
==29114==
==29114== LEAK SUMMARY:
==29114==    definitely lost: 80 bytes in 1 blocks
==29114==    indirectly lost: 68 bytes in 2 blocks
==29114==      possibly lost: 0 bytes in 0 blocks
==29114==    still reachable: 0 bytes in 0 blocks
==29114==         suppressed: 22,027 bytes in 184 blocks
==29114==
==29114== For counts of detected and suppressed errors, rerun with: -v
==29114== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 18 from 18)

根据valgrind泄漏内存,运行时的valgrind输出 --leak-check = full是

var num = 12000000

switch num {
case 1000...999999:
    print(String(num/1000) + "Kcs")
case 1000000...999999999:
    print(String(num/1000000) + "Mcs")
default:
    print(num)
}

要么我误解了某些东西,要么snprintf似乎在泄漏。

如果重要的话,我会使用os x。

编辑:我对“绝对丢失”和“间接丢失”内存泄漏感兴趣,而不是被抑制的内存(这不是泄漏)。

0 个答案:

没有答案