Skip to content

Commit

Permalink
fix shm handle shared resource crash bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjingchao-cn authored and fengqikai1414 committed Dec 4, 2017
1 parent e3cf4a2 commit 29d6b04
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions ros/ros_comm/roscpp/include/ros/shm_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class ShmManager
bool started_;
boost::interprocess::interprocess_mutex shm_sub_mutex_;
std::map<std::string, bool> shm_skip_first_msg_;
boost::mutex shm_map_mutex_;
boost::mutex shm_first_msg_map_mutex_;
};

}
Expand Down
21 changes: 17 additions & 4 deletions ros/ros_comm/roscpp/src/libros/shm_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,13 @@ void ShmManager::threadFunc()
item.datatype = sharedmem_util.get_datatype(segment);
item.message_definition = sharedmem_util.get_msg_def(segment);

boost::mutex::scoped_lock lock(shm_map_mutex_);

shm_map_[topic] = item;
}

boost::mutex::scoped_lock lock(shm_first_msg_map_mutex_);

if (shm_skip_first_msg_.find(topic) == shm_skip_first_msg_.end())
{
if (segment == NULL)
Expand Down Expand Up @@ -242,12 +246,21 @@ void ShmManager::threadFunc()
if (read_index == sharedmem_transport::ROS_SHM_SEGMENT_WROTE_NUM ||
shm_map_[topic].shm_sub_ptr->get_publisher_links().size() == 0)
{
if (shm_map_[topic].addr_sub)
{
delete [](shm_map_[topic].addr_sub);
boost::mutex::scoped_lock lock(shm_map_mutex_);

if (shm_map_[topic].addr_sub)
{
delete [](shm_map_[topic].addr_sub);
}
shm_map_.erase(topic);
}

{
boost::mutex::scoped_lock lock(shm_first_msg_map_mutex_);
shm_skip_first_msg_.erase(topic);
}
shm_map_.erase(topic);
shm_skip_first_msg_.erase(topic);

is_new_msg = false;
exit = true;
}
Expand Down

0 comments on commit 29d6b04

Please sign in to comment.