[pywin32-checkins] /hgrepo/p/py/pywin32/pywin32: fix some refcount edge cases, from...
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: <pyw...@li...> - 2012-01-02 01:41:56
|
changeset 95b4f896b100 in /hgrepo/p/py/pywin32/pywin32 details: http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/hgrepo/p/py/pywin32/pywin32?cmd=changeset;node=95b4f896b100 summary: fix some refcount edge cases, from kxroberto via #3440738 diffstat: Pythonwin/win32assoc.cpp | 6 ++++-- Pythonwin/win32cmd.cpp | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diffs (46 lines): diff -r dbbfddbcc824 -r 95b4f896b100 Pythonwin/win32assoc.cpp --- a/Pythonwin/win32assoc.cpp Sun Jan 01 14:01:21 2012 -0500 +++ b/Pythonwin/win32assoc.cpp Mon Jan 02 12:40:31 2012 +1100 @@ -225,14 +225,16 @@ // decref of the instance may trigger instance delete, // which may trigger AttachObject(None), which will // attempt to decref etc. - // So set the instance to NULL _before_ we decref it! + // So set the instance to NULL _before_ we decref it, and only + // do the decref after we've incref'd the new object - if it is the + // same object we may otherwise transition it via a refcount of 0. PyObject *old = pAssoc->virtualInst; pAssoc->virtualInst = NULL; - XDODECREF(old); if (ob!=Py_None) { pAssoc->virtualInst = ob; DOINCREF(ob); } + XDODECREF(old); RETURN_NONE; } diff -r dbbfddbcc824 -r 95b4f896b100 Pythonwin/win32cmd.cpp --- a/Pythonwin/win32cmd.cpp Sun Jan 01 14:01:21 2012 -0500 +++ b/Pythonwin/win32cmd.cpp Mon Jan 02 12:40:31 2012 +1100 @@ -208,8 +208,10 @@ RETURN_ERR("The parameter must be a callable object or None"); void *oldMethod = NULL; - // note I maybe decref, then maybe incref. I assume object wont be destroyed - // (ie, ref go to zero) between the 2 calls!) + // note I maybe decref, then maybe incref. To ensure the object will + // not be destroyed (ie, ref go to zero) between the 2 calls), I + // add a temporary reference first. + DOINCREF(hookedObject); if (pList->Lookup(message, oldMethod)) { pList->RemoveKey(message); // oldMethod is returned - don't drop its reference. @@ -220,6 +222,7 @@ pList->SetAt(message,method); Py_INCREF(hookedObject); } + DODECREF(hookedObject); // remove temp reference added above. if (oldMethod) return (PyObject *)oldMethod; else |