@@ -118,9 +118,10 @@ class cpp_function : public function {
118
118
119
119
// / Space optimization: don't inline this frequently instantiated fragment
120
120
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);
124
125
}
125
126
126
127
// / Special internal constructor for functors, lambda functions, etc.
@@ -490,8 +491,7 @@ class cpp_function : public function {
490
491
}
491
492
492
493
// / 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 ) {
495
495
// If on Python 3.9, check the interpreter "MICRO" (patch) version.
496
496
// If this is running on 3.9.0, we have to work around a bug.
497
497
#if !defined(PYPY_VERSION) && PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 9
@@ -504,7 +504,8 @@ class cpp_function : public function {
504
504
rec->free_data (rec);
505
505
// During initialization, these strings might not have been copied yet,
506
506
// 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) {
508
509
std::free ((char *) rec->name );
509
510
std::free ((char *) rec->doc );
510
511
std::free ((char *) rec->signature );
0 commit comments