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

Margarita Manterola margamanterola at gmail.com
Thu Mar 5 22:00:56 WST 2009


Hi!

I've already submitted a bug, with the whole information about this,
but I'm writting also to the list to see if someone has any idea of
how to fix it.

This is a problem with the C wrapper of gtk_window_list_toplevels for PyGTK.

This is the reported bug:
http://bugzilla.gnome.org/show_bug.cgi?id=574259 (it has stacktraces
and the like)

The problem is that in this code for _wrap_gtk_window_list_toplevels:

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

   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);
   return py_list;
}

The garbage collector is called while the code is in the for loop, it
removes one of the toplevel windows which is not in use anywhere,
except that it's already on the list, thus after the GC returns and
the loop tries to access that removed window, it triggers a segfault.

I can think of one ugly solution, but no correct solution.  The ugly
solution would be to add extra checks everywhere, so that if a window
was destroyed in the middle of the function, everything would still
work.  The correct solution would be to somehow prevent the GC from
destroying the unused window which is still in the list, but I have no
idea how.

Any ideas on how to fix it?

--
Besos,
Marga


More information about the pygtk mailing list