Skip to content

Commit

Permalink
Python: Prevent crash on finalization
Browse files Browse the repository at this point in the history
  • Loading branch information
linas committed Oct 3, 2013
1 parent c117d4e commit 67c5ea6
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions opencog/cython/PyMindAgent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,27 @@ const ClassInfo& PyMindAgent::classinfo() const
return _ci;
}

static bool in_fini = false;
#if __GNUC__
static __attribute__ ((destructor (65535))) void pyagent_fini(void)
{
in_fini = true;
}
#endif

PyMindAgent::~PyMindAgent()
{
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
// Do nothing if we are in finalizer ... because at this point,
// python is probably already dead, and doing the below will just
// segfault.
if (in_fini) return;

// Still fails XXX don't know how to fix this...
// Maybe we can ask python if its been finalized?
return;

// decrement python object reference counter
PyGILState_STATE gstate = PyGILState_Ensure();
Py_DECREF(pyagent);
PyGILState_Release(gstate);
}
Expand Down

0 comments on commit 67c5ea6

Please sign in to comment.