Q:

Why do the following program leaks memory?

I tried to write my first GTK+-program. Compilation went fine but valgrind says that there are memory leaks. I'm unable to find those so could anyone say what am I doing wrong? Or is it possible at all to write graphical Linux programs without memory leaks?
#include <gtk/gtk.h>

int main(int argc, char* argv[])
{
        gtk_init(&argc, &argv);

        GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title(GTK_WINDOW(window), "Hello World");

        gtk_container_set_border_width(GTK_CONTAINER(window), 60);

        GtkWidget* label = gtk_label_new("Hello, world!");
        gtk_container_add(GTK_CONTAINER(window), label);

        g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

        gtk_widget_show_all(window);

        gtk_main();

        return 0;
}

gcc -Wall gtkhello.c -o gtkhello $(pkg-config --cflags --libs gtk+-2.0)

valgrind -v ./gtkhello

...
==9395== HEAP SUMMARY:
==9395==     in use at exit: 538,930 bytes in 6,547 blocks
==9395==   total heap usage: 21,434 allocs, 14,887 frees, 2,964,543 bytes allocated
==9395== 
==9395== Searching for pointers to 6,547 not-freed blocks
==9395== Checked 949,656 bytes
==9395== 
==9395== LEAK SUMMARY:
==9395==    definitely lost: 4,480 bytes in 30 blocks
==9395==    indirectly lost: 5,160 bytes in 256 blocks
==9395==      possibly lost: 180,879 bytes in 1,716 blocks
==9395==    still reachable: 348,411 bytes in 4,545 blocks
==9395==         suppressed: 0 bytes in 0 blocks
==9395== Rerun with --leak-check=full to see details of leaked memory
==9395== 
==9395== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==9395== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

A:

You're not doing anything wrong. GTK widgets use reference counting, but in your programme all the references are taken care of, so you're not (manually) leaking anything.
So why does Valgrind claim you are?
Firstly, GLib has its own "slab" memory allocator, called GSlice, which is generally faster than system malloc for small allocations. Unfortunately, it confuses Valgrind, but if you set the environment variable G_SLICE=always-malloc, GSlice is effectively turned off.
Secondly, you can set the G_DEBUG=gc-friendly, which is supposed to help Valgrind produce more accurate results (although it my experience it generally doesn't make any difference).
Both of these environment variables are listed in the GLib documentation.
Unfortunately, even if you do both of these things, Valgrind will still report that your app leaks memory. The reason for this is that GTK (and its underlying libraries) allocate some "static" memory at start-up that doesn't get freed until the programme quits. This isn't really a problem, because typically the programme ends as soon as gtk_main() returns, and then the OS frees any remaining resources, so you're not really leaking anything. Valgrind thinks you are though, and for this reason it would be nice to have a gtk_deinit() function, but sadly there isn't one.
The best you can do instead is to teach Valgrind to ignore these things, via a suppressions file. The Valgrind page on the Gnome Wiki has details on all this and more.

