G_print() makes valgrind to give to many false results where printf() does not

Because GLib is a complex library, and performs various one-off allocations; also because g_print() is not just a macro around fprintf(), so it performs its own allocations.

The documentation should make it pretty clear:

Outputs a formatted message via the print handler. The default print handler simply outputs the message to stdout, without appending a trailing new-line character. Typically, format should end with its own new-line character.

g_print() should not be used from within libraries for debugging messages, since it may be redirected by applications to special purpose message windows or even files. Instead, libraries should use g_log() , g_log_structured() , or the convenience macros g_message() , g_warning() and g_error() .

If you are linking against GLib, and you’re using Valgrind, you should use the GLib suppression file; it’s called glib.supp and usually installed under /usr/share/glib-2.0/valgrind/—assuming you’re using the system copy of GLib:

valgrind \
  --leak-check=full \
  --track-origins=yes \
  --show-reachable=yes \
  --suppressions=/usr/share/glib-2.0/valgrind/glib.supp \
  ./program

Various libraries ship their own suppression files.

1 Like