2

This simple program

///test.c
#include <stdio.h>

int main(){
  double d = 13.37;
  char buf[128];
  snprintf(buf, 128, "%f", d);

  return 0;
}

is leaking memory according to valgrind, the valgrind output when run with --leak-check=full is

==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)

Either I'm misunderstanding something or snprintf seems to be leaking.

I'm on os x if it matters.

Edit: im interested in the "definitely lost" and "indirectly lost" memory leak not the suppressed memory (which isn't a leak).

  • 1
    Valgrind is known to have lots of false positives on OSX. Above is one of them. But if you do a search (which you should always do before posting a question) you will find many more. – kaylum Jul 16 '16 at 22:47
  • 1
    Can't reproduce with valgrind-3.11.0 and gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010. – Gluttton Jul 16 '16 at 22:50
  • @kaylum Thanks, but the suppressed memory leakage that person is asking about is different (and omnipresent). I've never run in to false "definitely lost" memory before with valgrind on os x. – user3516751 Jul 16 '16 at 22:52
  • 2
    Like I said, do a search if that dup doesn't suit. There are plenty of other examples of valgrind false positives on OSX. For example [this one](http://stackoverflow.com/questions/27842968/valgrind-reports-errors-for-a-very-simple-c-program) shows definitely lost false positives: – kaylum Jul 16 '16 at 22:55

0 Answers0