Skip to content

Commit

Permalink
Str mem leak rebased (ros2#185)
Browse files Browse the repository at this point in the history
* * 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>
  • Loading branch information
gaoethan authored and mikaelarguedas committed Nov 17, 2017
1 parent b40eff9 commit 54d06f5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
3 changes: 3 additions & 0 deletions rcl/include/rcl/error_handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_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_get_error_state rcutils_get_error_state
Expand Down
10 changes: 2 additions & 8 deletions rcl/src/rcl/expand_topic_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,14 @@ rcl_expand_topic_name(
*output_topic_name = NULL;
char * unmatched_substitution =
rcutils_strndup(next_opening_brace, substitution_substr_len, allocator);
char * allocated_msg = NULL;
char * msg = NULL;
if (unmatched_substitution) {
allocated_msg = rcutils_format_string(
RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
allocator,
"unknown substitution: %s", unmatched_substitution);
msg = allocated_msg;
} else {
RCUTILS_SAFE_FWRITE_TO_STDERR("failed to allocate memory for error message\n");
msg = "unknown substitution: allocation failed when reporting error";
RCUTILS_SAFE_FWRITE_TO_STDERR("failed to allocate memory for unmatched substitution\n");
}
RCL_SET_ERROR_MSG(msg, allocator)
allocator.deallocate(unmatched_substitution, allocator.state);
allocator.deallocate(allocated_msg, allocator.state);
allocator.deallocate(local_output, allocator.state);
return RCL_RET_UNKNOWN_SUBSTITUTION;
}
Expand Down
10 changes: 4 additions & 6 deletions rcl_lifecycle/src/transition_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ rcl_lifecycle_register_state(
const rcutils_allocator_t * allocator)
{
if (rcl_lifecycle_get_state(transition_map, state.id) != NULL) {
char * error_msg = rcutils_format_string(rcutils_get_default_allocator(),
"state %u is already registered\n", state.id);
RCL_SET_ERROR_MSG(error_msg, rcutils_get_default_allocator());
RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(rcutils_get_default_allocator(),
"state %u is already registered\n", state.id);
return RCL_RET_ERROR;
}

Expand Down Expand Up @@ -115,9 +114,8 @@ rcl_lifecycle_register_transition(

rcl_lifecycle_state_t * state = rcl_lifecycle_get_state(transition_map, transition.start->id);
if (!state) {
char * error_msg = rcutils_format_string(rcl_get_default_allocator(),
"state %u is not registered\n", transition.start->id);
RCL_SET_ERROR_MSG(error_msg, rcl_get_default_allocator());
RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(rcl_get_default_allocator(),
"state %u is not registered\n", transition.start->id);
return RCL_RET_ERROR;
}

Expand Down

0 comments on commit 54d06f5

Please sign in to comment.