Skip to content
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

Don't return pointers to static objects with return_value_policy::take_ownership. #3946

Merged
merged 2 commits into from
May 24, 2022

Conversation

MaartenBaert
Copy link
Contributor

@MaartenBaert MaartenBaert commented May 16, 2022

This fixes -Wfree-nonheap-object warnings produced by GCC.

Description

When compiling the pybind tests with the latest GCC, the following warnings are produced:

In function ‘cast’,
    inlined from ‘operator()’ at pybind11/include/pybind11/pybind11.h:249:33,
    inlined from ‘_FUN’ at pybind11/include/pybind11/pybind11.h:224:21:
pybind11/include/pybind11/detail/../cast.h:653:13: warning: ‘operator delete’ called on unallocated object ‘int_string_pair’ [-Wfree-nonheap-object]
  653 |             delete src;
      |             ^
pybind11/include/pybind11/detail/../cast.h: In function ‘_FUN’:
pybind11/tests/test_builtin_casters.cpp:269:40: note: declared here
  269 |     static std::pair<int, std::string> int_string_pair{2, "items"};
      |                                        ^
In function ‘cast’,
    inlined from ‘operator()’ at pybind11/include/pybind11/pybind11.h:249:33,
    inlined from ‘_FUN’ at pybind11/include/pybind11/pybind11.h:224:21:
pybind11/include/pybind11/stl.h:190:5: warning: ‘operator delete’ called on unallocated object ‘lvv’ [-Wfree-nonheap-object]
  190 |     PYBIND11_TYPE_CASTER(Type, const_name("List[") + value_conv::name + const_name("]"));
      |     ^
pybind11/include/pybind11/stl.h: In function ‘_FUN’:
pybind11/tests/test_stl.cpp:179: note: declared here
  179 |     static std::vector<RValueCaster> lvv{2};
      | 

Apparently two test cases were returning pointers to static objects with return_value_policy::take_ownership, which causes pybind to delete these objects later. This commit fixes that by allocating a copy and returning it instead of a pointer to the original.

Suggested changelog entry:

Fix ``-Wfree-nonheap-object`` warnings produced by GCC by avoiding returning pointers to static objects with ``return_value_policy::take_ownership``.

…e_ownership.

This fixes -Wfree-nonheap-object warnings produced by GCC.
Copy link
Collaborator

@Skylion007 Skylion007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to check that pointers to STL containers are castable.
Nvm, forgot new returns a pointer. Thoughts @rwgk ?

@Skylion007 Skylion007 self-requested a review May 16, 2022 18:04
Copy link
Collaborator

@Skylion007 Skylion007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@henryiii @rwgk Can we get a 2nd pair of eyes?

@rwgk
Copy link
Collaborator

rwgk commented May 16, 2022

We still need to check that pointers to STL containers are castable. Nvm, forgot new returns a pointer. Thoughts @rwgk ?

First thought: Huh? Why didn't ASAN and valgrind catch this? Pretty surprising, glad you discovered this.

Second thought: py::return_value_policy::reference would seem like a better fix. Is there a catch with that?

@Skylion007
Copy link
Collaborator

Yeah, I agree that changing the return_value_policy seems better.

@Skylion007 Skylion007 merged commit 4624e8e into pybind:master May 24, 2022
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label May 24, 2022
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants