@@ -20,6 +20,7 @@ extern "C"
2020#include "rcl/init.h"
2121
2222#include "./arguments_impl.h"
23+ #include "./common.h"
2324#include "./context_impl.h"
2425#include "./init_options_impl.h"
2526#include "rcl/arguments.h"
@@ -69,11 +70,10 @@ rcl_init(
6970 context -> global_arguments = rcl_get_zero_initialized_arguments ();
7071
7172 // Setup impl for context.
72- context -> impl = allocator .allocate (sizeof (rcl_context_impl_t ), allocator .state );
73+ // use zero_allocate so the cleanup function will not try to clean up uninitialized parts later
74+ context -> impl = allocator .zero_allocate (1 , sizeof (rcl_context_impl_t ), allocator .state );
7375 RCL_CHECK_FOR_NULL_WITH_MSG (
7476 context -> impl , "failed to allocate memory for context impl" , return RCL_RET_BAD_ALLOC );
75- // memset to 0 so the cleanup function will not try to clean up uninitialized parts later
76- memset (context -> impl , 0x0 , sizeof (rcl_context_impl_t ));
7777
7878 // Copy the options into the context for future reference.
7979 rcl_ret_t ret = rcl_init_options_copy (options , & (context -> impl -> init_options ));
@@ -86,12 +86,11 @@ rcl_init(
8686 context -> impl -> argc = argc ;
8787 context -> impl -> argv = NULL ;
8888 if (0 != argc && argv != NULL ) {
89- context -> impl -> argv = (char * * )allocator .allocate ( sizeof (char * ) * argc , allocator .state );
89+ context -> impl -> argv = (char * * )allocator .zero_allocate ( argc , sizeof (char * ), allocator .state );
9090 RCL_CHECK_FOR_NULL_WITH_MSG (
9191 context -> impl -> argv ,
9292 "failed to allocate memory for argv" ,
9393 fail_ret = RCL_RET_BAD_ALLOC ; goto fail );
94- memset (context -> impl -> argv , 0 , sizeof (char * ) * argc );
9594 int64_t i ;
9695 for (i = 0 ; i < argc ; ++ i ) {
9796 size_t argv_i_length = strlen (argv [i ]);
@@ -126,6 +125,7 @@ rcl_init(
126125 goto fail ;
127126 }
128127 rcutils_atomic_store ((atomic_uint_least64_t * )(& context -> instance_id_storage ), next_instance_id );
128+ context -> impl -> init_options .impl -> rmw_init_options .instance_id = next_instance_id ;
129129
130130 // Initialize rmw_init.
131131 context -> impl -> rmw_context = rmw_get_zero_initialized_context ();
@@ -134,7 +134,7 @@ rcl_init(
134134 & (context -> impl -> rmw_context ));
135135 if (RMW_RET_OK != rmw_ret ) {
136136 RCL_SET_ERROR_MSG (rmw_get_error_string ().str );
137- fail_ret = RCL_RET_ERROR ;
137+ fail_ret = rcl_convert_rmw_ret_to_rcl_ret ( rmw_ret ) ;
138138 goto fail ;
139139 }
140140
@@ -164,6 +164,12 @@ rcl_shutdown(rcl_context_t * context)
164164 // reset the instance id to 0 to indicate "invalid"
165165 rcutils_atomic_store ((atomic_uint_least64_t * )(& context -> instance_id_storage ), 0 );
166166
167+ rmw_ret_t rmw_ret = rmw_shutdown (& (context -> impl -> rmw_context ));
168+ if (RMW_RET_OK != rmw_ret ) {
169+ RCL_SET_ERROR_MSG (rmw_get_error_string ().str );
170+ return rcl_convert_rmw_ret_to_rcl_ret (rmw_ret );
171+ }
172+
167173 return RCL_RET_OK ;
168174}
169175
0 commit comments