Address reference leak issue (fixes #1029)

Creating an instance of of a pybind11-bound type caused a reference leak in the
associated Python type object, which could prevent these from being collected
upon interpreter shutdown. This commit fixes that issue for all types that are
defined in a scope (e.g. a module). Unscoped anonymous types (e.g. custom
iterator types) always retain a positive reference count to prevent their
collection.
This commit is contained in:
Wenzel Jakob
2017-08-25 16:02:18 +02:00
committed by GitHub
parent 8b40505575
commit c14c2762f6
2 changed files with 34 additions and 1 deletions

View File

@@ -202,3 +202,24 @@ def test_brace_initialization():
a = m.BraceInitialization(123, "test")
assert a.field1 == 123
assert a.field2 == "test"
@pytest.unsupported_on_pypy
def test_class_refcount():
"""Instances must correctly increase/decrease the reference count of their types (#1029)"""
from sys import getrefcount
class PyDog(m.Dog):
pass
for cls in m.Dog, PyDog:
refcount_1 = getrefcount(cls)
molly = [cls("Molly") for _ in range(10)]
refcount_2 = getrefcount(cls)
del molly
pytest.gc_collect()
refcount_3 = getrefcount(cls)
assert refcount_1 == refcount_3
assert refcount_2 > refcount_1