-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Shared Pointers and Protected Destructors #1178
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
Comments
The main issue here is that For example, the following (without pybind at all) doesn't compile: #include <memory>
class MyClass : public std::enable_shared_from_this<MyClass> {
public:
MyClass() {}
protected:
~MyClass() {}
};
int main() {
auto c = std::make_shared<MyClass>();
} I can't see how You'll likely have to implement your own smart pointer class which handles shared ownership and deletion via some other means. |
In this case, MyClass will be treated as an abstract class -- we only want to construct instances of its subtypes, all of which have public destructors. However, we still want to expose the inheritance hierarchy and make use of common methods that are defined in MyClass. Wouldn't it be possible to avoid generating a line that creates a shared pointer wrapper around MyClass (like std::make_shared()) and only do this for the subclasses? |
Thanks for your help btw. I understand that there is an inherent problem (independent of pybind) when you compile a line creating a shared pointer of MyClass. I'm just hoping that the error can be avoided when MyClass is used as an abstract class. |
Fixes pybind#1178 It's possible for a non-destructible base class to be declared; such a holder *can* be obtained via `std::enable_shared_from_this`, but a shared_ptr cannot be constructed from a returned pointer. This commit puts the holder constructor behind a `std::is_destructible` check, giving a runtime failure if we are ever given such a pointer without a valid `shared_from_this()` shared pointer.
Ah, I see the issue. Take a look at jagerman@53be819 - I think that should fix it, but feedback is welcome. |
Thanks! that seems to work. |
Could you perhaps merge this into master? |
Reminder, any chance we could merge this in? If not, would you mind if we submit a pull request based on your code? |
Yes, by all means submit it as a PR. (It also needs a test case added to the test suite to verify that it fixes what it's supposed to fix across the different compilers, but that should be fairly straightforward). |
I am trying to wrap a C++ class of the following type. The class has a protected destructor but should be containable in a shared_ptr due to the public inheritance from enable_shared_from_this. However, the pybind code produces an error becuase of the protected destructor.
The code also throws an error if I try to use the following pybind wrapper:
py::class_<MyClass, std::unique_ptr<MyClass, py::nodelete>> clsMyClass(mod, "MyClass");
The text was updated successfully, but these errors were encountered: