Skip to content

[BUG]: Using smart_holder in virtual inheritance relationship error #5786

Closed

Description

@MannixYang

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

3.0.0

Problem description

The following code about virtual inheritance relationship , i use smart_holder instead of std::shared_ptr.

class Animal {
public:
  virtual std::shared_ptr<Animal> clone() const = 0;
  virtual ~Animal() = default;
};

class Cat : virtual public Animal {
public:
  virtual ~Cat() override = default;
};

class Tiger : virtual public Cat {
public:
  Tiger() = default;
  Tiger(const Tiger &) = default;
  ~Tiger() override = default;
  std::shared_ptr<Animal> clone() const override {
    return std::make_shared<Tiger>(*this);
  }
};

void bind_animals(pybind11::module_ &module) {

  pybind11::class_<Animal, pybind11::smart_holder>(module, "Animal");

  pybind11::class_<Cat, Animal, pybind11::smart_holder>(module, "Cat");

  pybind11::class_<Tiger, Cat, pybind11::smart_holder>(
      module, "Tiger", pybind11::multiple_inheritance())
      .def(pybind11::init<>())
      .def("clone", &Tiger::clone);
}

In python code:

tiger = Tiger()
tiger.clone()

An error occurred:

due to a fatal error condition:
  SIGSEGV - Segmentation violation signal

But it is ok to use std::shared_ptr as py::class_ holder. Are there problems with my usage of the smart_holder.
Can anyone help me with this? It's urgent. Thank you very much!

Is this a regression? Put the last known working version here if it is.

Not a regression

Activity

  1. added
    triage
    on Aug 5, 2025
  2. rwgk commented on Aug 13, 2025

    @rwgk
    Collaborator

    Your reproducer looks valid. Yes, it should work, there is most likely some bug in the pybind11 smart_holder code.

    Could you do me a favor and open a PR with an added test?

    To get started, just create a new test_animal_cat_tiger.cpp and test_animal_cat_tiger.py pair of files under tests/ and add test_animal_cat_tiger to tests\CMakeLists.txt. It's really straightforward.

    Maybe copy the pair of test_class_sh_unique_ptr_member.cpp,py files, throw away almost all of the existing code, plug in your code. Make it work with shared_ptr as holder first, then plug in smart_holder instead and push to a PR.

    Maybe add [ci skip] to your top commit before pushing, so that our CI doesn't run unnecessarily.

    @henryiii Could you maybe advise what's the latest recommended cmake command to use to build and run just one test locally?

  3. MannixYang commented on Aug 13, 2025

    @MannixYang
    Author

    Your reproducer looks valid. Yes, it should work, there is most likely some bug in the pybind11 smart_holder code.

    Could you do me a favor and open a PR with an added test?

    To get started, just create a new test_animal_cat_tiger.cpp and test_animal_cat_tiger.py pair of files under tests/ and add test_animal_cat_tiger to tests\CMakeLists.txt. It's really straightforward.

    Maybe copy the pair of test_class_sh_unique_ptr_member.cpp,py files, throw away almost all of the existing code, plug in your code. Make it work with shared_ptr as holder first, then plug in smart_holder instead and push to a PR.

    Maybe add [ci skip] to your top commit before pushing, so that our CI doesn't run unnecessarily.

    @henryiii Could you maybe advise what's the latest recommended cmake command to use to build and run just one test locally?

    Thanks for your reply,I will do it

  4. henryiii commented on Aug 13, 2025

    @henryiii
    Collaborator

    To quickly run all the tests, you can set up your environment and then run cmake --workflow default, or if you have uv you can run cmake --workflow venv and let uv set up the environment for you. However, if you just want to run one test, you should split the commands up into their sub-parts (default or venv works here):

    cmake --preset default -DPYBIND11_TEST_OVERRIDE=test_animal_cat_tiger
    cmake --build --preset default
    cmake --build --preset tests

    (Note that tests -> testsvenv if you use venv. We don't support actual CTest yet, it's just another build preset for testing. I hope to fix that someday.) I might have slightly missed the override syntax, too, I think there's no .py extension on the selection.

  5. rwgk commented on Aug 14, 2025

    @rwgk
    Collaborator

    Thanks @henryiii

    FWIW, this worked for me, starting completely from scratch under Ubuntu 24.04:

    python3 -m venv Pybind11DevVenv
    . Pybind11DevVenv/bin/activate
    pip install --upgrade pip
    pip install cmake ninja uv
    

    To build and test everything:

    cmake --workflow venv
    

    Alternatively, building and running just one (existing) Python test:

    cmake --preset venv -DPYBIND11_TEST_OVERRIDE=test_class_sh_basic
    cmake --build --preset venv
    cmake --build --preset tests
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions