Open Bug 456864 Opened 16 years ago Updated 2 years ago

hook GLib/GObject reference counting functions into nsTraceRefcnt leak logging

Categories

(Core :: Widget: Gtk, defect)

x86
Linux
defect

Tracking

()

People

(Reporter: dbaron, Unassigned)

Details

Attachments

(3 files)

For a long time, I've wanted to hook up the GLib reference count functions (g_object_ref, g_object_unref) into nsTraceRefcnt leak logging, so that we can:
 * detect leaks of GLib objects more readily
 * debug those leaks more easily, using make-tree.pl, etc.
I have two patches lying around, dated September 26, 2003, from an approach for doing this that required patching glib.  Except for having to use a custom glib, this is slightly simpler than my newer approach since it requires hooking into fewer places.
Here's the beginning of an approach with no glib modifications.

This is a little bit tricky for two reasons;

(1) libgobject plays tricks with visibility, so we not only need to hook into g_object_ref and g_object_unref, but we need to hook into every public function that exposes unbalanced reference counts to the caller.  (This is because it uses hidden symbols internally and then aliases a default-visibility symbol to the same function.)  I haven't done this yet.

(2) getting the first reference at the right time is a bit tricky.  The approach I took here is a bit too late, since nsTraceRefcnt gets confused unless the first AddRef it gets for an object is the AddRef to 1.  If I could take the same approach to replace g_object_init (I think that's the relevant instance_init, although it's a parameter short, so maybe I'm confused) instead of g_object_constructor then things would be better -- assuming I can find a way to do that.
(In reply to comment #3)
> (1) libgobject plays tricks with visibility, so we not only need to hook into
> g_object_ref and g_object_unref, but we need to hook into every public function
> that exposes unbalanced reference counts to the caller.  (This is because it
> uses hidden symbols internally and then aliases a default-visibility symbol to
> the same function.)

I guess they don't build with -Bsymbolic then, but that would produce similar issues.

> (2) getting the first reference at the right time is a bit tricky.  The
> approach I took here is a bit too late, since nsTraceRefcnt gets confused
> unless the first AddRef it gets for an object is the AddRef to 1.

A quicky way to get around that could be to call NS_LogAddRef with
ref_count - 1 in g_object_ref until g_object_get_qdata finds a marker that is set when real_g_object_constructor returns.  The effects of g_object_set_qdata on memory use may distort other metrics though...
Summary: hook GLib reference counting functions into nsTraceRefcnt leak logging → hook GLib/GObject reference counting functions into nsTraceRefcnt leak logging
Perhaps if we only hooked _GObjectClass::constructor() and _GObjectClass::finalize(), it would at least allow us to detect leaks more easily, even though we wouldn't get info about all the points where references are taken.
Assignee: dbaron → nobody
Status: ASSIGNED → NEW
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: