Skip to content

Shared Pointers and Protected Destructors #1178

Description

@varun-intel

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");

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

namespace py = pybind11;
namespace {

class MyClass : public std::enable_shared_from_this<MyClass> {
protected:
	MyClass() {}
	~MyClass() {}
};

PYBIND11_MODULE(myclass, mod) {
    py::class_<MyClass, std::shared_ptr<MyClass>> clsMyClass(mod, "MyClass");
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions