Debugging reference count problems

The reference counting scheme used by GObject does solve quite a few memory management problems but also introduces new sources of bugs. In large applications, finding the exact spot where the reference count of an Object is not properly handled can be very difficult.

A useful tool in debugging reference counting problems is to set breakpoints in gdb on g_object_ref() and g_object_unref(). Once you know the address of the object you are interested in, you can make the breakpoints conditional:

break g_object_ref if _object == 0xcafebabe
break g_object_unref if _object == 0xcafebabe