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

Protect DepthCameraPlugin globals with a mutex #2949

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
19 changes: 10 additions & 9 deletions plugins/DepthCameraPlugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@
*/

#include <functional>
#include <mutex>
#include <unordered_map>

#include "plugins/DepthCameraPlugin.hh"

using namespace gazebo;
GZ_REGISTER_SENSOR_PLUGIN(DepthCameraPlugin)

std::mutex global_maps_mutex;

// Added this map to avoid breaking the ABI
static std::map<DepthCameraPlugin*, event::ConnectionPtr>
static std::unordered_map<DepthCameraPlugin*, event::ConnectionPtr>
connection_reflectance_map;
static std::map<DepthCameraPlugin*, event::ConnectionPtr>
static std::unordered_map<DepthCameraPlugin*, event::ConnectionPtr>
connection_normals_map;
/////////////////////////////////////////////////
DepthCameraPlugin::DepthCameraPlugin()
Expand All @@ -40,13 +44,9 @@ DepthCameraPlugin::~DepthCameraPlugin()
this->newRGBPointCloudConnection.reset();
this->newImageFrameConnection.reset();

std::map<DepthCameraPlugin*, event::ConnectionPtr>::iterator it;
it = connection_reflectance_map.find(this);
it->second.reset();
connection_reflectance_map.erase(it);
it = connection_normals_map.find(this);
it->second.reset();
connection_normals_map.erase(it);
std::lock_guard<std::mutex> guard{global_maps_mutex};
connection_reflectance_map.erase(this);
connection_normals_map.erase(this);

this->parentSensor.reset();
this->depthCamera.reset();
Expand Down Expand Up @@ -93,6 +93,7 @@ void DepthCameraPlugin::Load(sensors::SensorPtr _sensor,
std::placeholders::_3, std::placeholders::_4,
std::placeholders::_5));

std::lock_guard<std::mutex> guard{global_maps_mutex};
connection_reflectance_map.
insert(std::pair<DepthCameraPlugin*, event::ConnectionPtr>
(this, newReflectanceFrameConnection));
Expand Down