Skip to content

Commit

Permalink
free the old error state at the end of setting in case of recursive c…
Browse files Browse the repository at this point in the history
…alls
  • Loading branch information
wjwwood committed Feb 17, 2016
1 parent b8830f0 commit 08bddea
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions rmw/src/error_handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,10 @@ __rmw_reset_error(rmw_error_state_t * error_state);
void
rmw_set_error_state(const char * error_string, const char * file, size_t line_number)
{
if (rmw_error_is_set()) {
rmw_error_state_t * old_error_state = __rmw_error_state;
#if RMW_REPORT_ERROR_HANDLING_ERRORS
fprintf(
stderr,
"[rmw|error_handling.c:" RMW_STRINGIFY(__LINE__) "] error string being overwritten: %s\n",
rmw_get_error_string_safe());
const char * old_error_string = rmw_get_error_string_safe();
#endif
rmw_reset_error();
}
__rmw_error_state = (rmw_error_state_t *)rmw_allocate(sizeof(rmw_error_state_t));
if (!__rmw_error_state) {
#if RMW_REPORT_ERROR_HANDLING_ERRORS
Expand All @@ -62,7 +57,7 @@ rmw_set_error_state(const char * error_string, const char * file, size_t line_nu
__rmw_error_state->message = (char *)malloc(error_string_length + 1);
if (!__rmw_error_state->message) {
#if RMW_REPORT_ERROR_HANDLING_ERRORS
// rmw_allocate failed, but fwrite might work?
// malloc failed, but fwrite might work?
SAFE_FWRITE_TO_STDERR(
"[rmw|error_handling.c:" RMW_STRINGIFY(__LINE__)
"] failed to allocate memory for the error message in the error state struct\n");
Expand All @@ -86,6 +81,18 @@ rmw_set_error_state(const char * error_string, const char * file, size_t line_nu
#endif
__rmw_error_state->file = file;
__rmw_error_state->line_number = line_number;
if (__rmw_error_is_set(old_error_state)) {
#if RMW_REPORT_ERROR_HANDLING_ERRORS
// Only warn of overwritting if the new error string is different from the old ones.
if (error_string != old_error_string && error_string != old_error_state->message) {
fprintf(
stderr,
"[rmw|error_handling.c:" RMW_STRINGIFY(__LINE__) "] error string being overwritten: %s\n",
old_error_string);
}
#endif
__rmw_reset_error(old_error_state);
}
}

const rmw_error_state_t *
Expand Down Expand Up @@ -169,5 +176,5 @@ __rmw_reset_error(rmw_error_state_t * error_state)
void
rmw_reset_error()
{
return __rmw_reset_error(__rmw_error_state);
__rmw_reset_error(__rmw_error_state);
}

0 comments on commit 08bddea

Please sign in to comment.