Skip to content

Commit

Permalink
Yield rcl_context_fini() error codes. (#763)
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
  • Loading branch information
hidmic authored and ahcorde committed Oct 30, 2020
1 parent 63842fd commit 0c02c8d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions rcl/src/rcl/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern "C"

#include <stdbool.h>

#include "./common.h"
#include "./context_impl.h"
#include "rcutils/stdatomic_helper.h"

Expand Down Expand Up @@ -56,8 +57,7 @@ rcl_context_fini(rcl_context_t * context)
}
RCL_CHECK_ALLOCATOR_WITH_MSG(
&(context->impl->allocator), "invalid allocator", return RCL_RET_INVALID_ARGUMENT);
__cleanup_context(context);
return RCL_RET_OK;
return __cleanup_context(context);
}

// See `rcl_shutdown()` for invalidation of the context.
Expand Down Expand Up @@ -92,15 +92,16 @@ rcl_context_get_rmw_context(rcl_context_t * context)
return &(context->impl->rmw_context);
}

void
rcl_ret_t
__cleanup_context(rcl_context_t * context)
{
rcl_ret_t ret = RCL_RET_OK;
// reset the instance id to 0 to indicate "invalid" (should already be 0, but this is defensive)
rcutils_atomic_store((atomic_uint_least64_t *)(&context->instance_id_storage), 0);

// clean up global_arguments if initialized
if (NULL != context->global_arguments.impl) {
rcl_ret_t ret = rcl_arguments_fini(&(context->global_arguments));
ret = rcl_arguments_fini(&(context->global_arguments));
if (RCL_RET_OK != ret) {
RCUTILS_SAFE_FWRITE_TO_STDERR(
"[rcl|context.c:" RCUTILS_STRINGIFY(__LINE__)
Expand All @@ -118,8 +119,11 @@ __cleanup_context(rcl_context_t * context)

// finalize init options if valid
if (NULL != context->impl->init_options.impl) {
rcl_ret_t ret = rcl_init_options_fini(&(context->impl->init_options));
if (RCL_RET_OK != ret) {
rcl_ret_t init_options_fini_ret = rcl_init_options_fini(&(context->impl->init_options));
if (RCL_RET_OK != init_options_fini_ret) {
if (RCL_RET_OK == ret) {
ret = init_options_fini_ret;
}
RCUTILS_SAFE_FWRITE_TO_STDERR(
"[rcl|context.c:" RCUTILS_STRINGIFY(__LINE__)
"] failed to finalize init options while cleaning up context, memory may be leaked: ");
Expand All @@ -131,8 +135,11 @@ __cleanup_context(rcl_context_t * context)

// clean up rmw_context
if (NULL != context->impl->rmw_context.implementation_identifier) {
rmw_ret_t rmw_ret = rmw_context_fini(&(context->impl->rmw_context));
if (RMW_RET_OK != rmw_ret) {
rmw_ret_t rmw_context_fini_ret = rmw_context_fini(&(context->impl->rmw_context));
if (RMW_RET_OK != rmw_context_fini_ret) {
if (RCL_RET_OK == ret) {
ret = rcl_convert_rmw_ret_to_rcl_ret(rmw_context_fini_ret);
}
RCUTILS_SAFE_FWRITE_TO_STDERR(
"[rcl|context.c:" RCUTILS_STRINGIFY(__LINE__)
"] failed to finalize rmw context while cleaning up context, memory may be leaked: ");
Expand All @@ -157,6 +164,8 @@ __cleanup_context(rcl_context_t * context)

// zero-initialize the context
*context = rcl_get_zero_initialized_context();

return ret;
}

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion rcl/src/rcl/context_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ typedef struct rcl_context_impl_t
} rcl_context_impl_t;

RCL_LOCAL
void
rcl_ret_t
__cleanup_context(rcl_context_t * context);

#ifdef __cplusplus
Expand Down

0 comments on commit 0c02c8d

Please sign in to comment.