Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out-of-scope memory use and memory leak issue #172

Merged
merged 2 commits into from
Nov 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion rcl/src/rcl/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@ rcl_node_init(
// Have this special case to avoid a memory allocation when "" is passed.
local_namespace_ = "/";
}

char * temp = NULL;
// If the namespace does not start with a /, add one.
if (namespace_length > 0 && namespace_[0] != '/') {
// TODO(wjwwood): replace with generic strcat that takes an allocator once available
// length + 2, because new leading / and terminating \0
char * temp = (char *)allocator->allocate(namespace_length + 2, allocator->state);
temp = (char *)allocator->allocate(namespace_length + 2, allocator->state);
RCL_CHECK_FOR_NULL_WITH_MSG(
temp, "allocating memory failed", return RCL_RET_BAD_ALLOC, *allocator);
temp[0] = '/';
Expand All @@ -163,6 +165,9 @@ rcl_node_init(
ret = rmw_validate_namespace(local_namespace_, &validation_result, NULL);
if (ret != RMW_RET_OK) {
RCL_SET_ERROR_MSG(rmw_get_error_string_safe(), *allocator);
if (should_free_local_namespace_) {
allocator->deallocate((char *)local_namespace_, allocator->state);
}
return ret;
}
if (validation_result != RMW_NAMESPACE_VALID) {
Expand All @@ -176,11 +181,18 @@ rcl_node_init(
} else {
RCL_SET_ERROR_MSG(msg, *allocator);
}

if (should_free_local_namespace_) {
allocator->deallocate((char *)local_namespace_, allocator->state);
}
return RCL_RET_NODE_INVALID_NAMESPACE;
}

// Allocate space for the implementation struct.
node->impl = (rcl_node_impl_t *)allocator->allocate(sizeof(rcl_node_impl_t), allocator->state);
if (!node->impl && should_free_local_namespace_) {
allocator->deallocate((char *)local_namespace_, allocator->state);
}
RCL_CHECK_FOR_NULL_WITH_MSG(
node->impl, "allocating memory failed", return RCL_RET_BAD_ALLOC, *allocator);
node->impl->rmw_node_handle = NULL;
Expand Down Expand Up @@ -216,6 +228,9 @@ rcl_node_init(
RCL_SET_ERROR_MSG(
"Environment variable " RCUTILS_STRINGIFY(ROS_SECURITY_ENABLE_VAR_NAME)
" could not be read", rcl_get_default_allocator());
if (should_free_local_namespace_) {
allocator->deallocate((char *)local_namespace_, allocator->state);
}
return RCL_RET_ERROR;
}

Expand All @@ -225,6 +240,9 @@ rcl_node_init(
RCL_SET_ERROR_MSG(
"Environment variable " RCUTILS_STRINGIFY(ROS_SECURITY_STRATEGY_VAR_NAME)
" could not be read", rcl_get_default_allocator());
if (should_free_local_namespace_) {
allocator->deallocate((char *)local_namespace_, allocator->state);
}
return RCL_RET_ERROR;
}

Expand All @@ -246,6 +264,9 @@ rcl_node_init(
"SECURITY ERROR: unable to find a folder matching the node name in the "
RCUTILS_STRINGIFY(ROS_SECURITY_ROOT_DIRECTORY_VAR_NAME)
" directory while the requested security strategy requires it", *allocator);
if (should_free_local_namespace_) {
allocator->deallocate((char *)local_namespace_, allocator->state);
}
return RCL_RET_ERROR;
}
}
Expand All @@ -258,6 +279,7 @@ rcl_node_init(
// free local_namespace_ if necessary
if (should_free_local_namespace_) {
allocator->deallocate((char *)local_namespace_, allocator->state);
should_free_local_namespace_ = false;
}
// instance id
node->impl->rcl_instance_id = rcl_get_instance_id();
Expand Down Expand Up @@ -310,6 +332,11 @@ rcl_node_init(
allocator->deallocate(node->impl, allocator->state);
}
*node = rcl_get_zero_initialized_node();

if (should_free_local_namespace_) {
allocator->deallocate((char *)local_namespace_, allocator->state);
local_namespace_ = NULL;
}
return fail_ret;
}

Expand Down