mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user