Memory Leaks in GTK hello_world program

2019-01-12 08:54发布

问题:

So... I'm trying to eliminate some memory leaks from my GTK+ 3 program. I though it would be a good idea to look back at some simple examples to see if there is some cleanup stuff I'm forgetting, but the hello_world program provided in the documentation has leaks too. (Valgrind output below).

Are these leaks acceptable? If so, is there some other application I should be using to debug GTK programs?

==13717== Memcheck, a memory error detector
==13717== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==13717== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==13717== Command: ./a
==13717== 
Hello World
==13717== 
==13717== HEAP SUMMARY:
==13717==     in use at exit: 1,578,162 bytes in 11,614 blocks
==13717==   total heap usage: 45,699 allocs, 34,085 frees, 6,461,970 bytes allocated
==13717== 
==13717== LEAK SUMMARY:
==13717==    definitely lost: 2,560 bytes in 5 blocks
==13717==    indirectly lost: 6,656 bytes in 207 blocks
==13717==      possibly lost: 363,228 bytes in 1,937 blocks
==13717==    still reachable: 1,205,718 bytes in 9,465 blocks
==13717==         suppressed: 0 bytes in 0 blocks
==13717== Rerun with --leak-check=full to see details of leaked memory
==13717== 
==13717== For counts of detected and suppressed errors, rerun with: -v
==13717== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

Code:

#include <gtk/gtk.h>

/* This is a callback function. The data arguments are ignored
 * in this example. More on callbacks below.
 */
static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
}

static gboolean
on_delete_event (GtkWidget *widget,
                 GdkEvent  *event,
                 gpointer   data)
{
  /* If you return FALSE in the "delete_event" signal handler,
   * GTK will emit the "destroy" signal. Returning TRUE means
   * you don't want the window to be destroyed.
   *
   * This is useful for popping up 'are you sure you want to quit?'
   * type dialogs.
   */

  g_print ("delete event occurred\n");

  return TRUE;
}

int
main (int   argc,
      char *argv[])
{
  /* GtkWidget is the storage type for widgets */
  GtkWidget *window;
  GtkWidget *button;

  /* This is called in all GTK applications. Arguments are parsed
   * from the command line and are returned to the application.
   */
  gtk_init (&argc, &argv);

  /* create a new window, and set its title */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Hello");

  /* When the window emits the "delete-event" signal (which is emitted
   * by GTK+ in response to an event coming from the window manager,
   * usually as a result of clicking the "close" window control), we
   * ask it to call the on_delete_event() function as defined above.
   *
   * The data passed to the callback function is NULL and is ignored
   * in the callback function.
   */
  g_signal_connect (window, "delete-event", G_CALLBACK (on_delete_event), NULL);

  /* Here we connect the "destroy" event to the gtk_main_quit() function.
   *
   * This signal is emitted when we call gtk_widget_destroy() on the window,
   * or if we return FALSE in the "delete_event" callback.
   */
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

  /* Sets the border width of the window. */
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);

  /* Creates a new button with the label "Hello World". */
  button = gtk_button_new_with_label ("Hello World");

  /* When the button receives the "clicked" signal, it will call the
   * function print_hello() passing it NULL as its argument.
   *
   * The print_hello() function is defined above.
   */
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);

  /* The g_signal_connect_swapped() function will connect the "clicked" signal
   * of the button to the gtk_widget_destroy() function; instead of calling it
   * using the button as its argument, it will swap it with the user data
   * argument. This will cause the window to be destroyed by calling
   * gtk_widget_destroy() on the window.
   */
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);

  /* This packs the button into the window. A GtkWindow inherits from GtkBin,
   * which is a special container that can only have one child
   */
  gtk_container_add (GTK_CONTAINER (window), button);

  /* The final step is to display this newly created widget... */
  gtk_widget_show (button);

  /* ... and the window */
  gtk_widget_show (window);

  /* All GTK applications must have a gtk_main(). Control ends here
   * and waits for an event to occur (like a key press or a mouse event),
   * until gtk_main_quit() is called.
   */
  gtk_main ();

  return 0;
}

回答1:

This answer is compiled from answers to the same question (on the now defunct www.gtkforums.com).

GTK+ is pretty lazy when it comes to allocating and deallocating internal buffers needed for the life time of the application. For example it may allocate an area of memory for a lookup table during initialisation which is needed for the life of the application. GTK+ will then never deallocate this. To Valgrind this looks like a memory leak, (which technically it is) but as an optimisation GTK+ does not deallocate it as it will be deallocated during application exit and so not an error. This is why you need suppression files so that Valgrind can ignore these. The problem is that you will need to change these with most GTK+ version changes.

Repository of suppression files: https://github.com/dtrebbien/GNOME.supp

After cloning the repository, you can generate the suppression files (also comes with glib, gdk, and others) with "make" and then refer valgrind to them like so:

valgrind ./a --suppression=/path/to/gtk3.supp


回答2:

For debugging glib/gtk programs I would use this command:

G_SLICE=debug-blocks valgrind --tool=memcheck --leak-check=full <gtk program>

G_SLICE=debug-blocks will turn off gtk's advanced memory management to allow valgrind to show correct results.

--leak-check=full will show stack traces for the leaked memory blocks.

You can also use --show-reachable=yes to see stack traces for all memory blocks that haven't been free when the program exits.

There is also the massif valgrind tool that tracks memory usage to show which parts of the program are using the most memory.

Run program under massif:

G_SLICE=always-malloc valgrind --tool=massif --detailed-freq=2 --max-snapshots=400 --num-callers=20 <gtk program>

Show results:

ms_print massif.out.<pid>