Catch leaks in your unit tests

Let’s blog about my little trick that should be useful to everyone: Not everyone runs unit tests in valgrind, and even if they do it’s hard to see leaks from false-positive. Fortunately there is an easy way to catch GObject leaks in a unit test without using valgrind:

foo = my_object_new ();
g_object_add_weak_pointer (G_OBJECT (foo), (gpointer) &foo);
...
g_object_unref (foo);
g_assert (foo == NULL);

Do that on every object in your unit tests, it’s amazing what you can find…

2 thoughts on “Catch leaks in your unit tests”

Leave a Reply

Your email address will not be published. Required fields are marked *