Skip to content

Commit 4624e8e

Browse files
Don't return pointers to static objects with return_value_policy::take_ownership. (#3946)
* Don't return pointers to static objects with return_value_policy::take_ownership. This fixes -Wfree-nonheap-object warnings produced by GCC. * Use return value policy fix instead Co-authored-by: Aaron Gokaslan <[email protected]>
1 parent c42414d commit 4624e8e

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

tests/test_builtin_casters.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ TEST_SUBMODULE(builtin_casters, m) {
267267
m.def("lvalue_nested", []() -> const decltype(lvnested) & { return lvnested; });
268268

269269
static std::pair<int, std::string> int_string_pair{2, "items"};
270-
m.def("int_string_pair", []() { return &int_string_pair; });
270+
m.def(
271+
"int_string_pair", []() { return &int_string_pair; }, py::return_value_policy::reference);
271272

272273
// test_builtins_cast_return_none
273274
m.def("return_none_string", []() -> std::string * { return nullptr; });

tests/test_stl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ TEST_SUBMODULE(stl, m) {
177177
[](const std::vector<bool> &v) { return v.at(0) == true && v.at(1) == false; });
178178
// Unnumbered regression (caused by #936): pointers to stl containers aren't castable
179179
static std::vector<RValueCaster> lvv{2};
180-
m.def("cast_ptr_vector", []() { return &lvv; });
180+
m.def(
181+
"cast_ptr_vector", []() { return &lvv; }, py::return_value_policy::reference);
181182

182183
// test_deque
183184
m.def("cast_deque", []() { return std::deque<int>{1}; });

0 commit comments

Comments
 (0)