Skip to content

[BUG]: Segmentation fault on Windows with abstract classss #3217

Closed as not planned

Description

Required prerequisites

Problem description

A have two abstract C++ class: Door and Knob. Door returns a Knob with Door::GetKnob. The knob can be pulled with Knob::Pull(). Door successfully calls Door::GetKnob and gets a non-null knob, but the code crashes when Knob::Pull is called.

Reproducible example code

C++

#include <iostream>
#include <pybind11/pybind11.h>

namespace py = pybind11;

int simple_add(int i, int j) {
    return i + j;
}

class Knob {
 public:
  virtual void Pull() = 0;
};

class Door {
 public:
  virtual std::string_view Name() = 0;
  virtual Knob& GetKnob() = 0;
};

void open_door(Door& door) {
  std::string_view name = door.Name();
  std::cout << name << std::endl;
  Knob& knob = door.GetKnob();
  std::cout << "Got the knob" << std::endl;
  knob.Pull();
  std::cout << "Pulled the knob" << std::endl;
}

class PyKnob : public Knob {
 public:
  using Knob::Knob;
  void Pull() {
    PYBIND11_OVERRIDE_PURE(void, Knob, Pull);
  }
};

class PyDoor : public Door {
 public:
  using Door::Door;
  std::string_view Name() {
    PYBIND11_OVERRIDE_PURE(std::string_view, Door, Name);
  }
  Knob& GetKnob() {
    PYBIND11_OVERRIDE_PURE(Knob&, Door, GetKnob);
  }
};

PYBIND11_MODULE(pybind_test, m) {
  m.def("simple_add", &simple_add);
  m.def("open_door", &open_door);
  py::class_<Knob, PyKnob>(m, "Knob")
    .def(py::init<>())
    .def("Pull", &Knob::Pull);
  py::class_<Door, PyDoor>(m, "Door")
    .def(py::init<>())
    .def("Name", &Door::Name)
    .def("GetKnob", &Door::GetKnob);
}

Python

import pybind_test

print(pybind_test.simple_add(3, 4))

class MyKnob(pybind_test.Knob):
    def __init__(self):
        pybind_test.Knob.__init__(self)

    def Pull(self):
        print("Hey!")

class MyDoor(pybind_test.Door):
    def __init__(self, name):
        pybind_test.Door.__init__(self)
        self.name = name

    def Name(self):
        return self.name
    
    def GetKnob(self):
        return MyKnob()

print("Opening")
pybind_test.open_door(MyDoor("I'm a door."))
print("Done")

Output

7
Opening
I'm a door.
Got the knob

Program crashes at this point. (while calling knob.Pull();)

Activity

  1. TomWildenhain-Microsoft commented on Aug 25, 2021

    @TomWildenhain-Microsoft
    Author

    The problem appears to be that Knob is getting deallocated before it is returned by

        def GetKnob(self):
            return MyKnob()

    Setting a global
    MYKNOB = MyKnob()
    and returning it

        def GetKnob(self):
            return MYKNOB 

    works.

  2. TomWildenhain-Microsoft commented on Aug 25, 2021

    @TomWildenhain-Microsoft
    Author

    I tried the return value policies but it's a little tricky here, since the passing by reference means the C++ can't take ownership of the knob and python will always destroy the object if it is returned without referencing it elsewhere. The above hack actually works fairly well for my purposes. Feel free to close this if there is no better resolution.

    btw the classes page of the documentation should maybe link to the return value policies page.

  3. henryiii commented on Aug 26, 2021

    @henryiii
    Collaborator

    I get:

    RuntimeError: Tried to call pure virtual function "Knob::Pull"
    
  4. henryiii commented on Aug 26, 2021

    @henryiii
    Collaborator

    Also, this looks very problematic to me. You are returning a reference to an object you've created, but how does C++ know when to remove that object, and how does it know the object is actually larger than the reference you have (slicing issue)? If you can use smart pointers, I think this would be much simpler and easier. If not, I'd try to use move, not references. Expecting a user to remember to add a & for a return value is very error prone.

    In general, no references to virtual classes in C++. They get sliced. Always use (smart) pointers.

  5. laramiel commented on Sep 2, 2021

    @laramiel
    Contributor

    Try allocating the knob in the MyDoor constructor:

    class MyDoor(pybind_test.Door):
        def __init__(self, name):
            pybind_test.Door.__init__(self)
            self.name = name
            self.knob = MyKnob()
    
        def Name(self):
            return self.name
        
        def GetKnob(self):
            return self.knob

    Then at least you're not returning a temporary.

  6. TomWildenhain-Microsoft commented on Sep 3, 2021

    @TomWildenhain-Microsoft
    Author

    @laramiel yes that works but isn't the behavior I want. Pretend it's called "Knob factory" instead ;-). Also I tried shared_pointers and return value policies and couldn't get it to stop deallocating on return. The failure of shared_pointer really surprised me.

  7. henryiii commented on Sep 3, 2021

    @henryiii
    Collaborator

    I've already said - you can't return a reference to a temporary object with a virtual class. It doesn't work. The compiler slices off part of your object. It's UB.

    Using shared pointers should be just fine. Do you have the shared pointer version to look at?

  8. TomWildenhain-Microsoft commented on Sep 3, 2021

    @TomWildenhain-Microsoft
    Author

    Using shared pointers should be just fine. Do you have the shared pointer version to look at?

    Thanks for your help. Replace all Knob& with std::shared_ptr<Knob> to get the shared pointer version.

  9. TomWildenhain-Microsoft commented on Sep 21, 2021

    @TomWildenhain-Microsoft
    Author

    The cause of the issue is described here:
    #673 (comment)

    You were on the right track with shared_ptr. The reason you get the error about calling a virtual call() is that the Python object doesn't exist anymore at the time you're making the call. You still have the C++ instance, in the shared_ptr, but the Python instance (which was returned by A.create()) isn't around anymore because nothing references it once the call is over: pybind11 itself only holds on to it long enough to convert the value to a C++ type (std::shared_ptr in this case), which means that in your testing function, your t will be a shared pointer to a PyAnimal, but when pybind11 tries to look up the python instance associated with t to find the overridden call function there is no Python instance anymore.

    It would be great if pybind could keep the python instance around when there is a shared pointer to it.

  10. henryiii commented on Nov 23, 2026

    @henryiii
    Collaborator

    🤖 AI text below 🤖

    The segfault still happens on master, but it comes from the code in this issue, not from pybind11. The override returns Knob& to a Python object that is temporary. When the override returns, the Python object is destroyed, and C++ then uses a dangling reference. This is undefined behavior. A function that returns a reference must refer to an object that stays alive, for example one that the Python subclass stores as an attribute. Closing as not a pybind11 bug.

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