mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
Track base class pointers of instances
This commits adds base class pointers of offset base classes (i.e. due to multiple inheritance) to `registered_instances` so that if such a pointer is returned we properly recognize it as an existing instance. Without this, returning a base class pointer will cast to the existing instance if the pointer happens to coincide with the instance pointer, but constructs a new instance (quite possibly with a segfault, if ownership is applied) for unequal base class pointers due to multiple inheritance.
This commit is contained in:
@@ -112,6 +112,32 @@ def test_mi_dynamic_attributes():
|
||||
assert d.dynamic == 1
|
||||
|
||||
|
||||
def test_mi_unaligned_base():
|
||||
"""Returning an offset (non-first MI) base class pointer should recognize the instance"""
|
||||
from pybind11_tests import I801C, I801D, i801b1_c, i801b2_c, i801b1_d, i801b2_d
|
||||
|
||||
n_inst = ConstructorStats.detail_reg_inst()
|
||||
|
||||
c = I801C()
|
||||
d = I801D()
|
||||
# + 4 below because we have the two instances, and each instance has offset base I801B2
|
||||
assert ConstructorStats.detail_reg_inst() == n_inst + 4
|
||||
b1c = i801b1_c(c)
|
||||
assert b1c is c
|
||||
b2c = i801b2_c(c)
|
||||
assert b2c is c
|
||||
b1d = i801b1_d(d)
|
||||
assert b1d is d
|
||||
b2d = i801b2_d(d)
|
||||
assert b2d is d
|
||||
|
||||
assert ConstructorStats.detail_reg_inst() == n_inst + 4 # no extra instances
|
||||
del c, b1c, b2c
|
||||
assert ConstructorStats.detail_reg_inst() == n_inst + 2
|
||||
del d, b1d, b2d
|
||||
assert ConstructorStats.detail_reg_inst() == n_inst
|
||||
|
||||
|
||||
def test_mi_base_return():
|
||||
"""Tests returning an offset (non-first MI) base class pointer to a derived instance"""
|
||||
from pybind11_tests import (I801B2, I801C, I801D, i801c_b1, i801c_b2, i801d_b1, i801d_b2,
|
||||
@@ -129,7 +155,7 @@ def test_mi_base_return():
|
||||
assert d1.a == 1
|
||||
assert d1.b == 2
|
||||
|
||||
assert ConstructorStats.detail_reg_inst() == n_inst + 2
|
||||
assert ConstructorStats.detail_reg_inst() == n_inst + 4
|
||||
|
||||
c2 = i801c_b2()
|
||||
assert type(c2) is I801C
|
||||
@@ -141,10 +167,10 @@ def test_mi_base_return():
|
||||
assert d2.a == 1
|
||||
assert d2.b == 2
|
||||
|
||||
assert ConstructorStats.detail_reg_inst() == n_inst + 4
|
||||
assert ConstructorStats.detail_reg_inst() == n_inst + 8
|
||||
|
||||
del c2
|
||||
assert ConstructorStats.detail_reg_inst() == n_inst + 3
|
||||
assert ConstructorStats.detail_reg_inst() == n_inst + 6
|
||||
del c1, d1, d2
|
||||
assert ConstructorStats.detail_reg_inst() == n_inst
|
||||
|
||||
|
||||
Reference in New Issue
Block a user