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