@@ -70,7 +70,9 @@ pub struct DynamicMessageMetadata {
70
70
introspection_type_support_library : Arc < libloading:: Library > ,
71
71
type_support_ptr : * const rosidl_message_type_support_t ,
72
72
structure : MessageStructure ,
73
- fini_function : unsafe extern "C" fn ( * mut std:: os:: raw:: c_void ) ,
73
+ // The message content can be moved out into another message,
74
+ // in which case the drop impl is not responsible for calling fini anymore
75
+ fini_function : Option < unsafe extern "C" fn ( * mut std:: os:: raw:: c_void ) > ,
74
76
}
75
77
76
78
/// A message whose type is not known at compile-time.
@@ -85,9 +87,6 @@ pub struct DynamicMessage {
85
87
// This is aligned to the maximum possible alignment of a message (8)
86
88
// by the use of a special allocation function.
87
89
storage : Box < [ u8 ] > ,
88
- // This type allows moving the message contents out into another message,
89
- // in which case the drop impl is not responsible for calling fini anymore
90
- needs_fini : bool ,
91
90
}
92
91
93
92
// ========================= impl for DynamicMessagePackage =========================
@@ -201,16 +200,14 @@ impl DynamicMessagePackage {
201
200
unsafe { & * ( type_support. data as * const rosidl_message_members_t ) } ;
202
201
// SAFETY: The message members coming from a type support library will always be valid.
203
202
let structure = unsafe { MessageStructure :: from_rosidl_message_members ( message_members) } ;
204
- // The fini function will always exist.
205
- let fini_function = message_members. fini_function . unwrap ( ) ;
206
203
let metadata = DynamicMessageMetadata {
207
204
message_type,
208
205
introspection_type_support_library : Arc :: clone (
209
206
& self . introspection_type_support_library ,
210
207
) ,
211
208
type_support_ptr,
212
209
structure,
213
- fini_function,
210
+ fini_function : message_members . fini_function ,
214
211
} ;
215
212
Ok ( metadata)
216
213
}
@@ -314,7 +311,6 @@ impl DynamicMessageMetadata {
314
311
let dyn_msg = DynamicMessage {
315
312
metadata : self . clone ( ) ,
316
313
storage,
317
- needs_fini : true ,
318
314
} ;
319
315
Ok ( dyn_msg)
320
316
}
@@ -336,9 +332,9 @@ impl Deref for DynamicMessage {
336
332
337
333
impl Drop for DynamicMessage {
338
334
fn drop ( & mut self ) {
339
- if self . needs_fini {
335
+ if let Some ( fini_function ) = self . metadata . fini_function {
340
336
// SAFETY: The fini_function expects to be passed a pointer to the message
341
- unsafe { ( self . metadata . fini_function ) ( self . storage . as_mut_ptr ( ) as _ ) }
337
+ unsafe { ( fini_function) ( self . storage . as_mut_ptr ( ) as _ ) }
342
338
}
343
339
}
344
340
}
@@ -475,7 +471,7 @@ impl DynamicMessage {
475
471
dest_slice. copy_from_slice ( & self . storage ) ;
476
472
// Don't run the fini function on the src data anymore, because the inner parts would be
477
473
// double-freed by dst and src.
478
- self . needs_fini = false ;
474
+ self . metadata . fini_function = None ;
479
475
Ok ( dest)
480
476
} else {
481
477
Err ( self )
0 commit comments