From be85a6ada1e38b3c612a5235413a3f6d6b8eff03 Mon Sep 17 00:00:00 2001 From: David Rose Date: Wed, 26 Oct 2011 20:01:16 +0000 Subject: [PATCH] fix crash with threaded setPythonTag() --- panda/src/pgraph/pandaNode.cxx | 48 +++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/panda/src/pgraph/pandaNode.cxx b/panda/src/pgraph/pandaNode.cxx index 1a72c106c0..df05345f83 100644 --- a/panda/src/pgraph/pandaNode.cxx +++ b/panda/src/pgraph/pandaNode.cxx @@ -4635,12 +4635,23 @@ fillin(DatagramIterator &scan, BamReader *manager) { //////////////////////////////////////////////////////////////////// void PandaNode::CData:: inc_py_refs() { - PythonTagData::const_iterator ti; - for (ti = _python_tag_data.begin(); - ti != _python_tag_data.end(); - ++ti) { - PyObject *value = (*ti).second; - Py_XINCREF(value); + if (!_python_tag_data.empty()) { +#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) + // This might happen at any time, so be sure the Python state is + // ready for it. + PyGILState_STATE gstate; + gstate = PyGILState_Ensure(); +#endif + PythonTagData::const_iterator ti; + for (ti = _python_tag_data.begin(); + ti != _python_tag_data.end(); + ++ti) { + PyObject *value = (*ti).second; + Py_XINCREF(value); + } +#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) + PyGILState_Release(gstate); +#endif } } #endif // HAVE_PYTHON @@ -4654,12 +4665,25 @@ inc_py_refs() { //////////////////////////////////////////////////////////////////// void PandaNode::CData:: dec_py_refs() { - PythonTagData::const_iterator ti; - for (ti = _python_tag_data.begin(); - ti != _python_tag_data.end(); - ++ti) { - PyObject *value = (*ti).second; - Py_XDECREF(value); + if (!_python_tag_data.empty()) { +#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) + // This might happen at any time, so be sure the Python state is + // ready for it. + PyGILState_STATE gstate; + gstate = PyGILState_Ensure(); +#endif + + PythonTagData::const_iterator ti; + for (ti = _python_tag_data.begin(); + ti != _python_tag_data.end(); + ++ti) { + PyObject *value = (*ti).second; + Py_XDECREF(value); + } + +#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) + PyGILState_Release(gstate); +#endif } } #endif // HAVE_PYTHON