From 4624e8e16433f56c315abc9bfc49c449fdfe137f Mon Sep 17 00:00:00 2001 From: Maarten Baert Date: Tue, 24 May 2022 19:46:31 +0200 Subject: [PATCH] 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 --- tests/test_builtin_casters.cpp | 3 ++- tests/test_stl.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp index 7b67515e5..6529f47d0 100644 --- a/tests/test_builtin_casters.cpp +++ b/tests/test_builtin_casters.cpp @@ -267,7 +267,8 @@ TEST_SUBMODULE(builtin_casters, m) { m.def("lvalue_nested", []() -> const decltype(lvnested) & { return lvnested; }); static std::pair int_string_pair{2, "items"}; - m.def("int_string_pair", []() { return &int_string_pair; }); + m.def( + "int_string_pair", []() { return &int_string_pair; }, py::return_value_policy::reference); // test_builtins_cast_return_none m.def("return_none_string", []() -> std::string * { return nullptr; }); diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index b56a91953..38d32fda9 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -177,7 +177,8 @@ TEST_SUBMODULE(stl, m) { [](const std::vector &v) { return v.at(0) == true && v.at(1) == false; }); // Unnumbered regression (caused by #936): pointers to stl containers aren't castable static std::vector lvv{2}; - m.def("cast_ptr_vector", []() { return &lvv; }); + m.def( + "cast_ptr_vector", []() { return &lvv; }, py::return_value_policy::reference); // test_deque m.def("cast_deque", []() { return std::deque{1}; });