From 35c11d5f5a2cffa40ef5a5dc2d6ba962b7df3cce Mon Sep 17 00:00:00 2001 From: "kr.angelov" Date: Mon, 5 Jan 2015 11:24:00 +0000 Subject: [PATCH] fix the reference counting for callbacks in Python --- src/runtime/python/pypgf.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/runtime/python/pypgf.c b/src/runtime/python/pypgf.c index c38a483f7..4725695ff 100644 --- a/src/runtime/python/pypgf.c +++ b/src/runtime/python/pypgf.c @@ -1212,16 +1212,21 @@ pypgf_literal_callback_match(PgfLiteralCallback* self, PyObject* result = PyObject_CallFunction(callback->pycallback, "isi", lin_idx, sentence, *poffset); - if (result == NULL || result == Py_None) + if (result == NULL) return NULL; + if (result == Py_None) { + Py_DECREF(result); + return NULL; + } + PgfExprProb* ep = gu_new(PgfExprProb, out_pool); ExprObject* pyexpr; if (!PyArg_ParseTuple(result, "Ofi", &pyexpr, &ep->prob, poffset)) return NULL; ep->expr = pyexpr->expr; - + { // This is an uggly hack. We first show the expression ep->expr // and then we read it back but in out_pool. The whole purpose @@ -1251,7 +1256,7 @@ pypgf_literal_callback_match(PgfLiteralCallback* self, gu_pool_free(tmp_pool); } - Py_DECREF(pyexpr); + Py_DECREF(result); return ep; } @@ -1300,6 +1305,8 @@ pypgf_new_callbacks_map(PgfConcr* concr, PyObject *py_callbacks, callback->pycallback = pycallback; callback->fin.fn = pypgf_literal_callback_fin; + Py_XINCREF(callback->pycallback); + gu_pool_finally(pool, &callback->fin); pgf_callbacks_map_add_literal(concr, callbacks,