Skip to content

fix(stl): keep element casters alive when elements refer to them - #6283

Open
henryiii wants to merge 2 commits into
pybind:masterfrom
henryiii:fix/keep-subcasters-alive
Open

henryiii wants to merge 2 commits into
pybind:masterfrom
henryiii:fix/keep-subcasters-alive

Conversation

@henryiii

@henryiii henryiii commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Description

Fixes #2527 and fixes #2245. Alternative to and closes #6280, and replaces -- closes #2303 and closes #2532.

A container caster destroyed the type caster of each element before the bound function ran. Elements that refer to data in their caster were then dangling. Examples are the pointers in std::vector<const char *> and std::vector<std::string *>.

Changes:

  • New trait detail::cast_refers_to_caster<T>. It is true for the cast_is_temporary_value_reference<T> cases, and for a caster that has a static constexpr bool refers_to_caster = true member.
  • The container casters in stl.h (list, array, set, map, optional, variant) keep their element casters in a std::list for the duration of the call, but only if the trait is true. For other element types, the element caster stays a loop local, as before.
  • The container casters, tuple_caster and the reference_wrapper caster pass the flag up. Thus nested types such as std::vector<std::vector<const char *>> work.
  • A custom caster that returns a view of storage it owns (a span) can set the member. This is the case that [BUG] Keep pybind11 type casters alive recursively when needed #2527 asked for.

Compared with #6280:

  • This covers all element types, not only char *.
  • A plain const char * argument has no added cost.
  • A third-party container caster with its own load() loop is not fixed unless it uses element_casters. Casters that derive from the stl.h casters are fixed.

Trade-offs:

  • Each container caster has one more member, which is empty in the usual case.
  • A container caster whose elements refer to their casters is now move-only.
  • py::cast<std::vector<const char *>>(obj) still dangles, because the caster is a temporary there. docs/limitations.rst now says this. A static_assert is not added, because it would also reject py::cast<std::vector<PyObject *>>(), which works today.
  • Trampoline overrides that return such types do not change.

The tests include the cases from #6280. Tested locally with Apple Clang (C++17, full suite) and GCC 16 (C++11, test_stl and three related modules). The new test fails without the fix.

Suggested changelog entry:

  • Fixed dangling pointers when a container whose elements refer to their type caster (such as std::vector<const char *> or std::vector<std::string *>) is an argument of a bound function. A custom type caster can opt in with a refers_to_caster member.

📚 Documentation preview 📚: https://pybind11--6283.org.readthedocs.build/

A container caster destroyed the type caster of each element before the
bound function ran. Elements that refer to data in their caster, such as
the pointers in `std::vector<const char *>`, were then dangling.

Add the `cast_refers_to_caster` trait. The container casters keep the
element casters for the duration of the call only if the trait is true.
A custom caster can opt in with a `refers_to_caster` member.

Fixes pybind#2527, fixes pybind#2245.

Assisted-by: ClaudeCode:claude-fable-5-2
The variant caster value-initialized the caster of each alternative
before. With default initialization, GCC 16 reports
-Wmaybe-uninitialized for a `std::nullptr_t` alternative.

Assisted-by: ClaudeCode:claude-fable-5-2

This branch has not been deployed

No deployments
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.

[BUG] Keep pybind11 type casters alive recursively when needed passing containers of char * (e.g. std::vector<char *>) does not work

1 participant