Skip to content

Issue: object ref-count ignored when moving std::unique_ptr #2906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions tests/test_class_sh_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re

import pytest
import sys

from pybind11_tests import class_sh_basic as m

Expand Down Expand Up @@ -144,20 +145,24 @@ def test_unique_ptr_cref_roundtrip(num_round_trips=1000):
)
def test_unique_ptr_consumer_roundtrip(pass_f, rtrn_f, moved_out, moved_in):
c = m.uconsumer()
assert not c.valid()
recycled = m.atyp("passenger")
mtxt_orig = m.get_mtxt(recycled)
ptr_orig = m.get_ptr(recycled)
assert re.match("passenger_(MvCtor){1,2}", mtxt_orig)

__copy = recycled # noqa: F841
assert sys.getrefcount(recycled) == 3

pass_f(c, recycled)
if moved_out:
with pytest.raises(ValueError) as excinfo:
m.get_mtxt(recycled)
assert "Python instance was disowned" in str(excinfo.value)

recycled = rtrn_f(c)
assert c.valid() != moved_in
assert m.get_mtxt(recycled) == mtxt_orig
assert c.valid() != moved_in # consumer gave up ownership?
assert m.get_ptr(recycled) == ptr_orig # underlying C++ object never changes
assert m.get_mtxt(recycled) == mtxt_orig # object was not moved or copied


def test_py_type_handle_of_atyp():
Expand Down