Skip to content

Commit

Permalink
Protect DepthCameraPlugin globals with a mutex (#2949)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
  • Loading branch information
ivanpauno authored and scpeters committed Apr 21, 2021
1 parent c467d7f commit bf3ffb4
Showing 1 changed file with 10 additions and 9 deletions.
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

0 comments on commit bf3ffb4

Please sign in to comment.