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. Hopefully, there exist a tool named refdbg which can be used to automate the task of tracking down the location of invalid code with regard to reference counting. This application intercepts the reference counting calls and tries to detect invalid behavior. It supports a filter-rule mechanism to let you trace only the objects you are interested in and it can be used together with GDB.

Note that if GObject has been compiled with --enable-debug=yes, it exports a trap variable

static volatile GObject *g_trap_object_ref;
    

If set to a non-NULL value, g_object_ref() and g_object_unref() will be intercepted when called with that value.