Skip to content

Conversation

ManuelSchneid3r
Copy link
Contributor

@ManuelSchneid3r ManuelSchneid3r commented Oct 4, 2025

This PR is not meant to be merged but to provide a repoducer for the issue.

EDIT: Simplified the example and used the catch tests (embedded use case).

@rwgk
Copy link
Collaborator

rwgk commented Oct 5, 2025

The CI failures are because of this (caveat: I only looked at one log):

FAILED: [code=1] tests/CMakeFiles/pybind11_tests.dir/test_class_sh_virtual_py_cpp_mix.cpp.o 
/usr/bin/c++ -DPYBIND11_TEST_BOOST -DPYBIND11_TEST_EIGEN -Dpybind11_tests_EXPORTS -I/Users/runner/work/pybind11/pybind11/include -isystem /Users/runner/hostedtoolcache/Python/3.8.18/x64/include/python3.8 -isystem /Users/runner/work/pybind11/pybind11/build/_deps/eigen-src -Os -DNDEBUG -std=c++14 -fPIC -fvisibility=hidden -fcolor-diagnostics -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated -Wundef -Wnon-virtual-dtor -flto -Werror -MD -MT tests/CMakeFiles/pybind11_tests.dir/test_class_sh_virtual_py_cpp_mix.cpp.o -MF tests/CMakeFiles/pybind11_tests.dir/test_class_sh_virtual_py_cpp_mix.cpp.o.d -o tests/CMakeFiles/pybind11_tests.dir/test_class_sh_virtual_py_cpp_mix.cpp.o -c /Users/runner/work/pybind11/pybind11/tests/test_class_sh_virtual_py_cpp_mix.cpp
/Users/runner/work/pybind11/pybind11/tests/test_class_sh_virtual_py_cpp_mix.cpp:50:10: error: unused variable 'i' [-Werror,-Wunused-variable]
    auto i = container_vec->held->get(); // segfaults
         ^
1 error generated.

Interactive testing after fixing that:

================================================================================ FAILURES ================================================================================
__________________________________________________________________________ test_cpp_space_test ___________________________________________________________________________

    def test_cpp_space_test():
>       m.cpp_space_test(m.VirtualItemContainer(VirtualItemDerived()))
E       RuntimeError: Tried to call pure virtual function "VirtualItem::get"


test_class_sh_virtual_py_cpp_mix.py:78: RuntimeError
======================================================================== short test summary info =========================================================================
FAILED test_class_sh_virtual_py_cpp_mix.py::test_cpp_space_test - RuntimeError: Tried to call pure virtual function "VirtualItem::get"
====================================================================== 1 failed, 15 passed in 0.06s ======================================================================

To resolve that error:

diff --git a/tests/test_class_sh_virtual_py_cpp_mix.py b/tests/test_class_sh_virtual_py_cpp_mix.py
index 83e9152c..38549ae7 100644
--- a/tests/test_class_sh_virtual_py_cpp_mix.py
+++ b/tests/test_class_sh_virtual_py_cpp_mix.py
@@ -70,7 +70,7 @@ def test_get_from_cpp_unique_ptr(ctor, expected):


 class VirtualItemDerived(m.VirtualItem):
-    def getInt(self):
+    def get(self):
         return 42
========================================================================== test session starts ===========================================================================
platform linux -- Python 3.12.3, pytest-8.4.1, pluggy-1.6.0 -- /home/rgrossekunst/Venvs/misc/bin/python3
cachedir: .pytest_cache
installed packages of interest: numpy==2.3.1 scipy==1.16.0
C++ Info: 13.3.0 C++20 __pybind11_internals_v11_system_libstdcpp_gxx_abi_1xxx_use_cxx11_abi_1__ PYBIND11_SIMPLE_GIL_MANAGEMENT=False
rootdir: /home/rgrossekunst/forked/pybind11/tests
configfile: pytest.ini
plugins: xdist-3.8.0
collected 16 items

test_class_sh_virtual_py_cpp_mix.py::test_base_get[Base-101] PASSED                                                                                                [  6%]
test_class_sh_virtual_py_cpp_mix.py::test_base_get[PyBase-323] PASSED                                                                                              [ 12%]
test_class_sh_virtual_py_cpp_mix.py::test_base_get[CppDerivedPlain-202] PASSED                                                                                     [ 18%]
test_class_sh_virtual_py_cpp_mix.py::test_base_get[CppDerived-212] PASSED                                                                                          [ 25%]
test_class_sh_virtual_py_cpp_mix.py::test_base_get[PyCppDerived-434] PASSED                                                                                        [ 31%]
test_class_sh_virtual_py_cpp_mix.py::test_get_from_cpp_plainc_ptr[Base-4101] PASSED                                                                                [ 37%]
test_class_sh_virtual_py_cpp_mix.py::test_get_from_cpp_plainc_ptr[PyBase-4323] PASSED                                                                              [ 43%]
test_class_sh_virtual_py_cpp_mix.py::test_get_from_cpp_plainc_ptr[CppDerivedPlain-4202] PASSED                                                                     [ 50%]
test_class_sh_virtual_py_cpp_mix.py::test_get_from_cpp_plainc_ptr[CppDerived-4212] PASSED                                                                          [ 56%]
test_class_sh_virtual_py_cpp_mix.py::test_get_from_cpp_plainc_ptr[PyCppDerived-4434] PASSED                                                                        [ 62%]
test_class_sh_virtual_py_cpp_mix.py::test_get_from_cpp_unique_ptr[Base-5101] PASSED                                                                                [ 68%]
test_class_sh_virtual_py_cpp_mix.py::test_get_from_cpp_unique_ptr[PyBase-5323] PASSED                                                                              [ 75%]
test_class_sh_virtual_py_cpp_mix.py::test_get_from_cpp_unique_ptr[CppDerivedPlain-5202] PASSED                                                                     [ 81%]
test_class_sh_virtual_py_cpp_mix.py::test_get_from_cpp_unique_ptr[CppDerived-5212] PASSED                                                                          [ 87%]
test_class_sh_virtual_py_cpp_mix.py::test_get_from_cpp_unique_ptr[PyCppDerived-5434] PASSED                                                                        [ 93%]
test_class_sh_virtual_py_cpp_mix.py::test_cpp_space_test PASSED                                                                                                    [100%]

=========================================================================== 16 passed in 0.03s ===========================================================================

In case your original issue was something related but different, could you please make changes to this PR and ensure that we actually see that error in the CI?

@ManuelSchneid3r ManuelSchneid3r changed the title Test PR triggering RuntimeError: Tried to call pure virtual function "VirtualItem::get" Test PR triggering RuntimeError: Tried to call pure virtual function "…" Oct 6, 2025
@ManuelSchneid3r
Copy link
Contributor Author

sorry for the ping. Not ready yet.

@ManuelSchneid3r
Copy link
Contributor Author

Hi @rwgk, it took me days to find out why my usecase fails while the test I provided before does not.

In my project the trampoline did not inherit from the base class public. Since in my usecase the base is an interface (no non pure members) I had no problems with private inhertance (was not intended though). This leads to this weird misleading issue.

Some setups give hints, but some, like mine, do not.

Maybe you could add some static assertions or support private inheritance.

Whichever you prefer, but please do either of them to save future pybind users some severely frustrating time.

@ManuelSchneid3r ManuelSchneid3r changed the title Test PR triggering RuntimeError: Tried to call pure virtual function "…" Test PR: Private inheritance triggering RuntimeError: Tried to call pure virtual function "…" Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants