@@ -21,6 +21,7 @@ extern "C"
21
21
22
22
#include <stdbool.h>
23
23
24
+ #include "./common.h"
24
25
#include "./context_impl.h"
25
26
#include "rcutils/stdatomic_helper.h"
26
27
@@ -56,8 +57,7 @@ rcl_context_fini(rcl_context_t * context)
56
57
}
57
58
RCL_CHECK_ALLOCATOR_WITH_MSG (
58
59
& (context -> impl -> allocator ), "invalid allocator" , return RCL_RET_INVALID_ARGUMENT );
59
- __cleanup_context (context );
60
- return RCL_RET_OK ;
60
+ return __cleanup_context (context );
61
61
}
62
62
63
63
// See `rcl_shutdown()` for invalidation of the context.
@@ -103,15 +103,16 @@ rcl_context_get_rmw_context(rcl_context_t * context)
103
103
return & (context -> impl -> rmw_context );
104
104
}
105
105
106
- void
106
+ rcl_ret_t
107
107
__cleanup_context (rcl_context_t * context )
108
108
{
109
+ rcl_ret_t ret = RCL_RET_OK ;
109
110
// reset the instance id to 0 to indicate "invalid" (should already be 0, but this is defensive)
110
111
rcutils_atomic_store ((atomic_uint_least64_t * )(& context -> instance_id_storage ), 0 );
111
112
112
113
// clean up global_arguments if initialized
113
114
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 ));
115
116
if (RCL_RET_OK != ret ) {
116
117
RCUTILS_SAFE_FWRITE_TO_STDERR (
117
118
"[rcl|context.c:" RCUTILS_STRINGIFY (__LINE__ )
@@ -129,8 +130,11 @@ __cleanup_context(rcl_context_t * context)
129
130
130
131
// finalize init options if valid
131
132
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
+ }
134
138
RCUTILS_SAFE_FWRITE_TO_STDERR (
135
139
"[rcl|context.c:" RCUTILS_STRINGIFY (__LINE__ )
136
140
"] failed to finalize init options while cleaning up context, memory may be leaked: " );
@@ -142,8 +146,11 @@ __cleanup_context(rcl_context_t * context)
142
146
143
147
// clean up rmw_context
144
148
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
+ }
147
154
RCUTILS_SAFE_FWRITE_TO_STDERR (
148
155
"[rcl|context.c:" RCUTILS_STRINGIFY (__LINE__ )
149
156
"] failed to finalize rmw context while cleaning up context, memory may be leaked: " );
@@ -168,6 +175,8 @@ __cleanup_context(rcl_context_t * context)
168
175
169
176
// zero-initialize the context
170
177
* context = rcl_get_zero_initialized_context ();
178
+
179
+ return ret ;
171
180
}
172
181
173
182
#ifdef __cplusplus
0 commit comments