[smart_holder] Keep parent alive when returning raw pointers (#4609)

* Avoid dangling pointers.

* Add test for const ptr

* Fix test failures.

* Fix ClangTidy

* fix emplace_back
This commit is contained in:
Xiaofei Wang
2023-04-06 13:18:05 -07:00
committed by GitHub
parent b37a1cd01d
commit 99cf27a4f5
3 changed files with 66 additions and 1 deletions

View File

@@ -715,7 +715,11 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
static handle cast(T *src, return_value_policy policy, handle parent) {
if (policy == return_value_policy::_clif_automatic) {
policy = return_value_policy::reference;
if (parent) {
policy = return_value_policy::reference_internal;
} else {
policy = return_value_policy::reference;
}
}
return cast(const_cast<T const *>(src), policy, parent); // Mutbl2Const
}