Skip to content

Commit 247d61a

Browse files
committed
Change DeleteStrings template argument to free_strings runtime argument in destruct(function_record *)
1 parent 0f70076 commit 247d61a

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

include/pybind11/pybind11.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ class cpp_function : public function {
118118

119119
/// Space optimization: don't inline this frequently instantiated fragment
120120
PYBIND11_NOINLINE unique_function_record make_function_record() {
121-
// `destruct<true>(function_record)`: `initialize_generic` copies strings and
122-
// takes care of cleaning up in case of exceptions.
123-
return unique_function_record(new detail::function_record(), destruct<false>);
121+
// `destruct(function_record, false)`: `initialize_generic` copies strings and
122+
// takes care of cleaning up in case of exceptions. So set `free_srings` to `false`.
123+
auto destruct_no_free_strings = [](detail::function_record * rec) { destruct(rec, false); };
124+
return unique_function_record(new detail::function_record(), destruct_no_free_strings);
124125
}
125126

126127
/// Special internal constructor for functors, lambda functions, etc.
@@ -490,8 +491,7 @@ class cpp_function : public function {
490491
}
491492

492493
/// When a cpp_function is GCed, release any memory allocated by pybind11
493-
template <bool DeleteStrings = true>
494-
static void destruct(detail::function_record *rec) {
494+
static void destruct(detail::function_record *rec, bool free_strings = true) {
495495
// If on Python 3.9, check the interpreter "MICRO" (patch) version.
496496
// If this is running on 3.9.0, we have to work around a bug.
497497
#if !defined(PYPY_VERSION) && PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 9
@@ -504,7 +504,8 @@ class cpp_function : public function {
504504
rec->free_data(rec);
505505
// During initialization, these strings might not have been copied yet,
506506
// so they cannot be freed. Once the function has been created, they can.
507-
if (DeleteStrings) {
507+
// Check `make_function_record` for more details.
508+
if (free_strings) {
508509
std::free((char *) rec->name);
509510
std::free((char *) rec->doc);
510511
std::free((char *) rec->signature);

0 commit comments

Comments
 (0)