Ticket #3670: pygobject.2.patch

File pygobject.2.patch, 1.6 KB (added by benzea, 12 years ago)

Tested patch, this fixes the crash.

  • gobject/pygobject.c

    diff --git a/gobject/pygobject.c b/gobject/pygobject.c
    index 70dc89a..d258a8e 100644
    a b GQuark pygobject_ref_sunk_key; 
    5858void
    5959pygobject_data_free(PyGObjectData *data)
    6060{
    61     PyGILState_STATE state = pyglib_gil_state_ensure();
     61    /* This function may be called after the python interpreter has already
     62     * been shut down. If this happens, we cannot do any python calls, so just
     63     * free the memory. */
     64    PyGILState_STATE state;
     65    PyThreadState *_save = NULL;
     66
    6267    GSList *closures, *tmp;
    63     Py_DECREF(data->type);
     68
     69    if (Py_IsInitialized()) {
     70        state = pyglib_gil_state_ensure();
     71        Py_DECREF(data->type);
     72
     73        /* We cannot use pyg_begin_allow_threads here because this is inside
     74         * a branch. */
     75        if (pyg_threads_enabled)
     76            _save = PyEval_SaveThread();
     77    }
     78
    6479    tmp = closures = data->closures;
    6580#ifndef NDEBUG
    6681    data->closures = NULL;
    6782    data->type = NULL;
    6883#endif
    69     pyg_begin_allow_threads;
     84
    7085    while (tmp) {
    7186        GClosure *closure = tmp->data;
    7287 
    pygobject_data_free(PyGObjectData *data) 
    7590        tmp = tmp->next;
    7691        g_closure_invalidate(closure);
    7792    }
    78     pyg_end_allow_threads;
    79  
     93
    8094    if (data->closures != NULL)
    8195        g_warning("invalidated all closures, but data->closures != NULL !");
    8296
    8397    g_free(data);
    84     pyglib_gil_state_release(state);
     98
     99    if (Py_IsInitialized()) {
     100        if (pyg_threads_enabled)
     101            PyEval_RestoreThread(_save);
     102
     103        pyglib_gil_state_release(state);
     104    }
    85105}
    86106
    87107static inline PyGObjectData *