@@ -21,6 +21,7 @@ extern "C"
2121
2222#include <stdbool.h>
2323
24+ #include "./common.h"
2425#include "./context_impl.h"
2526#include "rcutils/stdatomic_helper.h"
2627
@@ -56,8 +57,7 @@ rcl_context_fini(rcl_context_t * context)
5657 }
5758 RCL_CHECK_ALLOCATOR_WITH_MSG (
5859 & (context -> impl -> allocator ), "invalid allocator" , return RCL_RET_INVALID_ARGUMENT );
59- __cleanup_context (context );
60- return RCL_RET_OK ;
60+ return __cleanup_context (context );
6161}
6262
6363// See `rcl_shutdown()` for invalidation of the context.
@@ -103,15 +103,16 @@ rcl_context_get_rmw_context(rcl_context_t * context)
103103 return & (context -> impl -> rmw_context );
104104}
105105
106- void
106+ rcl_ret_t
107107__cleanup_context (rcl_context_t * context )
108108{
109+ rcl_ret_t ret = RCL_RET_OK ;
109110 // reset the instance id to 0 to indicate "invalid" (should already be 0, but this is defensive)
110111 rcutils_atomic_store ((atomic_uint_least64_t * )(& context -> instance_id_storage ), 0 );
111112
112113 // clean up global_arguments if initialized
113114 if (NULL != context -> global_arguments .impl ) {
114- rcl_ret_t ret = rcl_arguments_fini (& (context -> global_arguments ));
115+ ret = rcl_arguments_fini (& (context -> global_arguments ));
115116 if (RCL_RET_OK != ret ) {
116117 RCUTILS_SAFE_FWRITE_TO_STDERR (
117118 "[rcl|context.c:" RCUTILS_STRINGIFY (__LINE__ )
@@ -129,8 +130,11 @@ __cleanup_context(rcl_context_t * context)
129130
130131 // finalize init options if valid
131132 if (NULL != context -> impl -> init_options .impl ) {
132- rcl_ret_t ret = rcl_init_options_fini (& (context -> impl -> init_options ));
133- if (RCL_RET_OK != ret ) {
133+ rcl_ret_t init_options_fini_ret = rcl_init_options_fini (& (context -> impl -> init_options ));
134+ if (RCL_RET_OK != init_options_fini_ret ) {
135+ if (RCL_RET_OK == ret ) {
136+ ret = init_options_fini_ret ;
137+ }
134138 RCUTILS_SAFE_FWRITE_TO_STDERR (
135139 "[rcl|context.c:" RCUTILS_STRINGIFY (__LINE__ )
136140 "] failed to finalize init options while cleaning up context, memory may be leaked: " );
@@ -142,8 +146,11 @@ __cleanup_context(rcl_context_t * context)
142146
143147 // clean up rmw_context
144148 if (NULL != context -> impl -> rmw_context .implementation_identifier ) {
145- rmw_ret_t rmw_ret = rmw_context_fini (& (context -> impl -> rmw_context ));
146- if (RMW_RET_OK != rmw_ret ) {
149+ rmw_ret_t rmw_context_fini_ret = rmw_context_fini (& (context -> impl -> rmw_context ));
150+ if (RMW_RET_OK != rmw_context_fini_ret ) {
151+ if (RCL_RET_OK == ret ) {
152+ ret = rcl_convert_rmw_ret_to_rcl_ret (rmw_context_fini_ret );
153+ }
147154 RCUTILS_SAFE_FWRITE_TO_STDERR (
148155 "[rcl|context.c:" RCUTILS_STRINGIFY (__LINE__ )
149156 "] failed to finalize rmw context while cleaning up context, memory may be leaked: " );
@@ -168,6 +175,8 @@ __cleanup_context(rcl_context_t * context)
168175
169176 // zero-initialize the context
170177 * context = rcl_get_zero_initialized_context ();
178+
179+ return ret ;
171180}
172181
173182#ifdef __cplusplus
0 commit comments