@@ -115,8 +115,8 @@ class cpp_function : public function {
115
115
116
116
protected:
117
117
// / Space optimization: don't inline this frequently instantiated fragment
118
- PYBIND11_NOINLINE detail::function_record * make_function_record () {
119
- return new detail::function_record ();
118
+ PYBIND11_NOINLINE std::unique_ptr< detail::function_record> make_function_record () {
119
+ return std::unique_ptr<detail::function_record>( new detail::function_record () );
120
120
}
121
121
122
122
// / Special internal constructor for functors, lambda functions, etc.
@@ -126,7 +126,8 @@ class cpp_function : public function {
126
126
struct capture { remove_reference_t <Func> f; };
127
127
128
128
/* Store the function including any extra state it might have (e.g. a lambda capture object) */
129
- auto rec = make_function_record ();
129
+ auto unique_rec = make_function_record ();
130
+ auto rec = unique_rec.get ();
130
131
131
132
/* Store the capture object directly in the function record if there is enough space */
132
133
if (sizeof (capture) <= sizeof (rec->data )) {
@@ -207,7 +208,7 @@ class cpp_function : public function {
207
208
PYBIND11_DESCR_CONSTEXPR auto types = decltype (signature)::types ();
208
209
209
210
/* Register the function with Python from generic (non-templated) code */
210
- initialize_generic (rec , signature.text , types.data (), sizeof ...(Args));
211
+ initialize_generic (std::move (unique_rec) , signature.text , types.data (), sizeof ...(Args));
211
212
212
213
if (cast_in::has_args) rec->has_args = true ;
213
214
if (cast_in::has_kwargs) rec->has_kwargs = true ;
@@ -224,8 +225,9 @@ class cpp_function : public function {
224
225
}
225
226
226
227
// / Register a function call with Python (generic non-templated code goes here)
227
- void initialize_generic (detail::function_record *rec , const char *text,
228
+ void initialize_generic (std::unique_ptr< detail::function_record> &&unique_rec , const char *text,
228
229
const std::type_info *const *types, size_t args) {
230
+ auto rec = unique_rec.get ();
229
231
230
232
/* Create copies of all referenced C-style strings */
231
233
rec->name = strdup (rec->name ? rec->name : " " );
@@ -356,7 +358,7 @@ class cpp_function : public function {
356
358
rec->def ->ml_meth = reinterpret_cast <PyCFunction>(reinterpret_cast <void (*) (void )>(*dispatcher));
357
359
rec->def ->ml_flags = METH_VARARGS | METH_KEYWORDS;
358
360
359
- capsule rec_capsule (rec , [](void *ptr) {
361
+ capsule rec_capsule (unique_rec. release () , [](void *ptr) {
360
362
destruct ((detail::function_record *) ptr);
361
363
});
362
364
@@ -393,13 +395,13 @@ class cpp_function : public function {
393
395
chain_start = rec;
394
396
rec->next = chain;
395
397
auto rec_capsule = reinterpret_borrow<capsule>(((PyCFunctionObject *) m_ptr)->m_self );
396
- rec_capsule.set_pointer (rec );
398
+ rec_capsule.set_pointer (unique_rec. release () );
397
399
} else {
398
400
// Or end of chain (normal behavior)
399
401
chain_start = chain;
400
402
while (chain->next )
401
403
chain = chain->next ;
402
- chain->next = rec ;
404
+ chain->next = unique_rec. release () ;
403
405
}
404
406
}
405
407
0 commit comments