[pygtk] Problem with the GC starting in the middle of _wrap_gtk_window_list_toplevels

Margarita Manterola margamanterola at gmail.com
Fri Mar 6 02:38:07 WST 2009


On Thu, Mar 5, 2009 at 12:38 PM, Hrvoje Niksic <hrvoje.niksic at avl.com> wrote:

> I'm not sure about a correct fix, but it's a bit worrying that the C
> backtrace is showing no signs of GC.  Why not add code that disables the GC
> and reenables it around the loop?  Something like:
>
> // error checking elided for brevity
> static PyObject *gc_module;
> if (!gc_module)
>  gc_module = PyImport_Import("gc");
> PyObject *ret;
> ret = PyObject_CallMethod(gc_module, "disable", "");
> Py_DECREF(ret);
>
> ... loop goes here ...
>
> ret = PyObject_CallMethod(gc_module, "enable", "");
> Py_DECREF(ret);

I probably screwd up, because it didn't work.  This is what I was left with:

static PyObject *
_wrap_gtk_window_list_toplevels(PyGObject *self)
{
    GList *list, *tmp;
    PyObject *py_list;
    PyObject *gtk_obj;

    // error checking elided for brevity
    PyObject *ret;
    static PyObject *gc_module;
    if (!gc_module)
        gc_module = PyImport_Import("gc");
    ret = PyObject_CallMethod(gc_module, "disable", "");
    Py_DECREF(ret);

    list = gtk_window_list_toplevels();

    if ((py_list = PyList_New(0)) == NULL) {
        g_list_free(list);
        return NULL;
    }
    for (tmp = list; tmp != NULL; tmp = tmp->next) {
        gtk_obj = pygobject_new(G_OBJECT(tmp->data));
        if (gtk_obj == NULL) {
            g_list_free(list);
            Py_DECREF(py_list);
            return NULL;
        }
        PyList_Append(py_list, gtk_obj);
        Py_DECREF(gtk_obj);
    }
    g_list_free(list);
    ret = PyObject_CallMethod(gc_module, "enable", "");
    Py_DECREF(ret);
    return py_list;

}

But this segfaults as soon as the function is called:

#0  PyObject_CallFunctionObjArgs (callable=0xb7ebbe6c) at
../Objects/abstract.c:2042
#1  0x080e5fed in PyImport_Import (module_name=0xb7b2da9c) at
../Python/import.c:2576
#2  0xb7ac987c in _wrap_gtk_window_list_toplevels (self=0x0) at
gtkwindow.override:39

What did I do wrong?

> Maybe you're seeing a weird corner case of
> http://bugzilla.gnome.org/show_bug.cgi?id=546802 which is known to cause GC
> to prematurely break cycles which it shouldn't.  I'd try applying the fix
> for that bug, maybe it helps.

I applied that patch and it seems to have worked.  No more segfaults,
no calls to the tooltips_destroy.

However, I have no idea what the patch does and if the bug is really
fixed by this, or it's just a coincidence (being that it's a bug
related to what might happen if GC runs, it's very difficult to
check).

-- 
Besos,
Marga


More information about the pygtk mailing list