diff options
author | Renato Filho <renato.filho@openbossa.org> | 2011-06-17 18:10:21 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:15:27 -0300 |
commit | 88a84ee6ae8cd12b2527576505f16774c9c542c1 (patch) | |
tree | 64449426c1b79c4647c9f94f09543d7eda4f046b | |
parent | c3eb8bb198b7cbc43236b1edbf7afbf096d8b8f6 (diff) |
Fix refleak during the parent c++ object destruction.
Fixes bug #893.
Reviewer: Luciano Wolf <luciano.wolf@openbossa.org>
Renato Araújo <renato.filho@openbossa.org>
-rw-r--r-- | libshiboken/basewrapper.cpp | 14 | ||||
-rw-r--r-- | tests/libsample/objecttype.h | 14 | ||||
-rw-r--r-- | tests/samplebinding/objecttype_test.py | 18 | ||||
-rw-r--r-- | tests/samplebinding/typesystem_sample.xml | 9 |
4 files changed, 53 insertions, 2 deletions
diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp index 79bd9bd5e..9552031ff 100644 --- a/libshiboken/basewrapper.cpp +++ b/libshiboken/basewrapper.cpp @@ -960,6 +960,11 @@ void removeParent(SbkObject* child, bool giveOwnershipBack, bool keepReference) { ParentInfo* pInfo = child->d->parentInfo; if (!pInfo || !pInfo->parent) { + if (pInfo && pInfo->hasWrapperRef) { + pInfo->hasWrapperRef = false; + delete pInfo; + child->d->parentInfo = 0; + } return; } ChildrenList& oldBrothers = pInfo->parent->d->parentInfo->children; @@ -973,8 +978,15 @@ void removeParent(SbkObject* child, bool giveOwnershipBack, bool keepReference) pInfo->parent = 0; // This will keep the wrapper reference, will wait for wrapper destruction to remove that - if (keepReference && child->d->containsCppWrapper) + if (keepReference && + child->d->containsCppWrapper) { + //If have already a extra ref remove this one + if (pInfo->hasWrapperRef) + Py_CLEAR(child); + else + pInfo->hasWrapperRef = true; return; + } // Transfer ownership back to Python child->d->hasOwnership = giveOwnershipBack; diff --git a/tests/libsample/objecttype.h b/tests/libsample/objecttype.h index a5abf7067..5cc029e85 100644 --- a/tests/libsample/objecttype.h +++ b/tests/libsample/objecttype.h @@ -107,6 +107,20 @@ public: void callVirtualCreateChild(); virtual ObjectType* createChild(ObjectType* parent); + //return a parent from C++ + ObjectType* getCppParent() { + if (!m_parent) { + ObjectType* parent = new ObjectType(); + setParent(parent); + } + return m_parent; + } + + void destroyCppParent() { + delete m_parent; + m_parent = 0; + } + private: ObjectType(const ObjectType&); ObjectType& operator=(const ObjectType&); diff --git a/tests/samplebinding/objecttype_test.py b/tests/samplebinding/objecttype_test.py index 2a0dbbdb8..2448a7f68 100644 --- a/tests/samplebinding/objecttype_test.py +++ b/tests/samplebinding/objecttype_test.py @@ -26,8 +26,8 @@ '''Tests ObjectType class of object-type with privates copy constructor and = operator.''' -import sys import unittest +import sys from sample import ObjectType, Str @@ -62,6 +62,22 @@ class ObjectTypeTest(unittest.TestCase): o.setNullObject(None) self.assertEqual(o.callId(), 1) + def testParentFromCpp(self): + o = ObjectType() + self.assertEqual(sys.getrefcount(o), 2) + o.getCppParent().setObjectName('parent') + self.assertEqual(sys.getrefcount(o), 3) + o.getCppParent().setObjectName('parent') + self.assertEqual(sys.getrefcount(o), 3) + o.getCppParent().setObjectName('parent') + self.assertEqual(sys.getrefcount(o), 3) + o.getCppParent().setObjectName('parent') + self.assertEqual(sys.getrefcount(o), 3) + o.getCppParent().setObjectName('parent') + self.assertEqual(sys.getrefcount(o), 3) + o.destroyCppParent() + self.assertEqual(sys.getrefcount(o), 2) + if __name__ == '__main__': unittest.main() diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index 2537b6047..d99ee0fbc 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -173,6 +173,15 @@ <!-- rename function to avoid Python signature conflit --> <modify-function signature="setObject(const Null&)" rename="setNullObject" /> + <modify-function signature="getCppParent()"> + <modify-argument index="this"> + <parent index="return" action="add" /> + </modify-argument> + <modify-argument index="return"> + <define-ownership class="target" owner="default"/> + </modify-argument> + </modify-function> + <modify-function signature="event(Event*)"> <modify-argument index="1" invalidate-after-use="yes"/> </modify-function> |