-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix various minor memory leaks in the tests (found by Valgrind in #2746) #2758
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
Changes from all commits
b14891c
16bece7
aaeddc3
26d048a
10635e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -231,7 +231,8 @@ TEST_SUBMODULE(class_, m) { | |||||
}; | ||||||
|
||||||
auto def = new PyMethodDef{"f", f, METH_VARARGS, nullptr}; | ||||||
return py::reinterpret_steal<py::object>(PyCFunction_NewEx(def, nullptr, m.ptr())); | ||||||
py::capsule def_capsule(def, [](void *ptr) { delete reinterpret_cast<PyMethodDef *>(ptr); }); | ||||||
return py::reinterpret_steal<py::object>(PyCFunction_NewEx(def, def_capsule.ptr(), m.ptr())); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
What this changes is Beyond that, I have no idea how this works. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We already do this in the actual pybind11 code, btw. Alternatively, if you prefer, we could play the " There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm just trying to understand how does setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does borrow, yes, and keeps it alive as some kind of CPython-style pybind11/include/pybind11/pybind11.h Line 492 in d587a2f
See here for the other case where CPython uses it: pybind11/include/pybind11/pybind11.h Line 372 in d587a2f
This mechanism was also our problem with Python 3.9.0, |
||||||
}()); | ||||||
|
||||||
// test_operator_new_delete | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,6 +214,7 @@ TEST_SUBMODULE(copy_move_policies, m) { | |
}; | ||
py::class_<MoveIssue2>(m, "MoveIssue2").def(py::init<int>()).def_readwrite("value", &MoveIssue2::v); | ||
|
||
m.def("get_moveissue1", [](int i) { return new MoveIssue1(i); }, py::return_value_policy::move); | ||
// #2742: Don't expect ownership of raw pointer to `new`ed object to be transferred with `py::return_value_policy::move` | ||
m.def("get_moveissue1", [](int i) { return std::unique_ptr<MoveIssue1>(new MoveIssue1(i)); }, py::return_value_policy::move); | ||
Comment on lines
+217
to
+218
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to make things clear, this is avoiding the issue reported in #2742, but not proposing a definite solution. Moreover, the issue that was a precursor to adding this tests doesn't seem to depend on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, but if we decide to have a different approach to #2742, we'll add new tests for those, anyway? So this change could be semi-permanent? |
||
m.def("get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,13 +86,13 @@ TEST_SUBMODULE(stl_binders, m) { | |
|
||
// test_noncopyable_containers | ||
py::bind_vector<std::vector<E_nc>>(m, "VectorENC"); | ||
m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments from #2746:
|
||
m.def("get_vnc", &one_to_n<std::vector<E_nc>>); | ||
py::bind_vector<std::deque<E_nc>>(m, "DequeENC"); | ||
m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference); | ||
m.def("get_dnc", &one_to_n<std::deque<E_nc>>); | ||
py::bind_map<std::map<int, E_nc>>(m, "MapENC"); | ||
m.def("get_mnc", ×_ten<std::map<int, E_nc>>, py::return_value_policy::reference); | ||
m.def("get_mnc", ×_ten<std::map<int, E_nc>>); | ||
py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC"); | ||
m.def("get_umnc", ×_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference); | ||
m.def("get_umnc", ×_ten<std::unordered_map<int, E_nc>>); | ||
// Issue #1885: binding nested std::map<X, Container<E>> with E non-copyable | ||
py::bind_map<std::map<int, std::vector<E_nc>>>(m, "MapVecENC"); | ||
m.def("get_nvnc", [](int n) | ||
|
@@ -102,11 +102,11 @@ TEST_SUBMODULE(stl_binders, m) { | |
for (int j = 1; j <= n; j++) | ||
(*m)[i].emplace_back(j); | ||
return m; | ||
}, py::return_value_policy::reference); | ||
}); | ||
py::bind_map<std::map<int, std::map<int, E_nc>>>(m, "MapMapENC"); | ||
m.def("get_nmnc", ×_hundred<std::map<int, std::map<int, E_nc>>>, py::return_value_policy::reference); | ||
m.def("get_nmnc", ×_hundred<std::map<int, std::map<int, E_nc>>>); | ||
py::bind_map<std::unordered_map<int, std::unordered_map<int, E_nc>>>(m, "UmapUmapENC"); | ||
m.def("get_numnc", ×_hundred<std::unordered_map<int, std::unordered_map<int, E_nc>>>, py::return_value_policy::reference); | ||
m.def("get_numnc", ×_hundred<std::unordered_map<int, std::unordered_map<int, E_nc>>>); | ||
|
||
// test_vector_buffer | ||
py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment from #2746: