Str mem leak rebased (#185)

* * memory leak issues

address those memory leak issues with the API
rcutils_set_formatted_error which is defined
in rcutils

Signed-off-by: Ethan Gao <ethan.gao@linux.intel.com>

* * Address those memory leak issues with new MACRO
add macro RCL_SET_ERROR_MSG_WITH_FORMAT_STRING which
is equals RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING and
fix the memory leak issues with it

Signed-off-by: Ethan Gao <ethan.gao@linux.intel.com>

* address uncrustify grumble

Signed-off-by: Ethan Gao <ethan.gao@linux.intel.com>
This commit is contained in:
Ethan Gao 2017-11-17 15:25:02 +08:00 committed by Mikael Arguedas
parent b40eff9b9e
commit 54d06f5654
3 changed files with 9 additions and 14 deletions

View file

@ -45,6 +45,9 @@ typedef rcutils_error_state_t rcl_error_state_t;
#define RCL_SET_ERROR_MSG(msg, allocator) RCUTILS_SET_ERROR_MSG(msg, allocator) #define RCL_SET_ERROR_MSG(msg, allocator) RCUTILS_SET_ERROR_MSG(msg, allocator)
#define RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(allocator, fmt_str, ...) \
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(allocator, fmt_str, __VA_ARGS__)
#define rcl_error_is_set rcutils_error_is_set #define rcl_error_is_set rcutils_error_is_set
#define rcl_get_error_state rcutils_get_error_state #define rcl_get_error_state rcutils_get_error_state

View file

@ -166,20 +166,14 @@ rcl_expand_topic_name(
*output_topic_name = NULL; *output_topic_name = NULL;
char * unmatched_substitution = char * unmatched_substitution =
rcutils_strndup(next_opening_brace, substitution_substr_len, allocator); rcutils_strndup(next_opening_brace, substitution_substr_len, allocator);
char * allocated_msg = NULL;
char * msg = NULL;
if (unmatched_substitution) { if (unmatched_substitution) {
allocated_msg = rcutils_format_string( RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
allocator, allocator,
"unknown substitution: %s", unmatched_substitution); "unknown substitution: %s", unmatched_substitution);
msg = allocated_msg;
} else { } else {
RCUTILS_SAFE_FWRITE_TO_STDERR("failed to allocate memory for error message\n"); RCUTILS_SAFE_FWRITE_TO_STDERR("failed to allocate memory for unmatched substitution\n");
msg = "unknown substitution: allocation failed when reporting error";
} }
RCL_SET_ERROR_MSG(msg, allocator)
allocator.deallocate(unmatched_substitution, allocator.state); allocator.deallocate(unmatched_substitution, allocator.state);
allocator.deallocate(allocated_msg, allocator.state);
allocator.deallocate(local_output, allocator.state); allocator.deallocate(local_output, allocator.state);
return RCL_RET_UNKNOWN_SUBSTITUTION; return RCL_RET_UNKNOWN_SUBSTITUTION;
} }

View file

@ -77,9 +77,8 @@ rcl_lifecycle_register_state(
const rcutils_allocator_t * allocator) const rcutils_allocator_t * allocator)
{ {
if (rcl_lifecycle_get_state(transition_map, state.id) != NULL) { if (rcl_lifecycle_get_state(transition_map, state.id) != NULL) {
char * error_msg = rcutils_format_string(rcutils_get_default_allocator(), RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(rcutils_get_default_allocator(),
"state %u is already registered\n", state.id); "state %u is already registered\n", state.id);
RCL_SET_ERROR_MSG(error_msg, rcutils_get_default_allocator());
return RCL_RET_ERROR; return RCL_RET_ERROR;
} }
@ -115,9 +114,8 @@ rcl_lifecycle_register_transition(
rcl_lifecycle_state_t * state = rcl_lifecycle_get_state(transition_map, transition.start->id); rcl_lifecycle_state_t * state = rcl_lifecycle_get_state(transition_map, transition.start->id);
if (!state) { if (!state) {
char * error_msg = rcutils_format_string(rcl_get_default_allocator(), RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(rcl_get_default_allocator(),
"state %u is not registered\n", transition.start->id); "state %u is not registered\n", transition.start->id);
RCL_SET_ERROR_MSG(error_msg, rcl_get_default_allocator());
return RCL_RET_ERROR; return RCL_RET_ERROR;
} }