mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
Hold strong references to keep_alive patients
This fixes #856. Instead of the weakref trick, the internals structure holds an unordered_map from PyObject* to a vector of references. To avoid the cost of the unordered_map lookup for objects that don't have any keep_alive patients, a flag is added to each instance to indicate whether there is anything to do.
This commit is contained in:
committed by
Jason Rhinelander
parent
2196696746
commit
9d698f7fcc
@@ -24,6 +24,13 @@ public:
|
||||
Child *returnNullChild() { return nullptr; }
|
||||
};
|
||||
|
||||
#if !defined(PYPY_VERSION)
|
||||
class ParentGC : public Parent {
|
||||
public:
|
||||
using Parent::Parent;
|
||||
};
|
||||
#endif
|
||||
|
||||
test_initializer keep_alive([](py::module &m) {
|
||||
py::class_<Parent>(m, "Parent")
|
||||
.def(py::init<>())
|
||||
@@ -34,6 +41,11 @@ test_initializer keep_alive([](py::module &m) {
|
||||
.def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>())
|
||||
.def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>());
|
||||
|
||||
#if !defined(PYPY_VERSION)
|
||||
py::class_<ParentGC, Parent>(m, "ParentGC", py::dynamic_attr())
|
||||
.def(py::init<>());
|
||||
#endif
|
||||
|
||||
py::class_<Child>(m, "Child")
|
||||
.def(py::init<>());
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user