Skip to content

Commit 4e40b36

Browse files
authored
Prevent crash on Plotting plugin with mutex (#747)
Signed-off-by: Louise Poubel <[email protected]>
1 parent f795b65 commit 4e40b36

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/gui/plugins/plotting/Plotting.cc

+17-5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ namespace ignition::gazebo
4949
/// map key: string contains EntityID + "," + ComponentID
5050
public: std::map<std::string,
5151
std::shared_ptr<PlotComponent>> components;
52+
53+
/// \brief Mutex to protect the components map.
54+
public: std::recursive_mutex componentsMutex;
5255
};
5356

5457
class PlotComponentPrivate
@@ -194,7 +197,7 @@ ComponentTypeId PlotComponent::TypeId()
194197
}
195198

196199
//////////////////////////////////////////////////
197-
Plotting ::Plotting() : GuiSystem(),
200+
Plotting::Plotting() : GuiSystem(),
198201
dataPtr(std::make_unique<PlottingPrivate>())
199202
{
200203
this->dataPtr->plottingIface = std::make_unique<gui::PlottingInterface>();
@@ -215,7 +218,7 @@ Plotting ::Plotting() : GuiSystem(),
215218
}
216219

217220
//////////////////////////////////////////////////
218-
Plotting ::~Plotting()
221+
Plotting::~Plotting()
219222
{
220223
}
221224

@@ -229,13 +232,16 @@ void Plotting::LoadConfig(const tinyxml2::XMLElement *)
229232
//////////////////////////////////////////////////
230233
void Plotting::SetData(std::string _Id, const ignition::math::Vector3d &_vector)
231234
{
235+
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->componentsMutex);
232236
this->dataPtr->components[_Id]->SetAttributeValue("x", _vector.X());
233237
this->dataPtr->components[_Id]->SetAttributeValue("y", _vector.Y());
234238
this->dataPtr->components[_Id]->SetAttributeValue("z", _vector.Z());
235239
}
236240

241+
//////////////////////////////////////////////////
237242
void Plotting::SetData(std::string _Id, const ignition::msgs::Light &_light)
238243
{
244+
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->componentsMutex);
239245
if (_light.has_specular())
240246
{
241247
this->dataPtr->components[_Id]->SetAttributeValue("specularR",
@@ -290,6 +296,7 @@ void Plotting::SetData(std::string _Id, const ignition::msgs::Light &_light)
290296
//////////////////////////////////////////////////
291297
void Plotting::SetData(std::string _Id, const ignition::math::Pose3d &_pose)
292298
{
299+
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->componentsMutex);
293300
this->dataPtr->components[_Id]->SetAttributeValue("x", _pose.Pos().X());
294301
this->dataPtr->components[_Id]->SetAttributeValue("y", _pose.Pos().Y());
295302
this->dataPtr->components[_Id]->SetAttributeValue("z", _pose.Pos().Z());
@@ -302,6 +309,7 @@ void Plotting::SetData(std::string _Id, const ignition::math::Pose3d &_pose)
302309
//////////////////////////////////////////////////
303310
void Plotting::SetData(std::string _Id, const sdf::Physics &_physics)
304311
{
312+
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->componentsMutex);
305313
this->dataPtr->components[_Id]->SetAttributeValue("stepSize",
306314
_physics.MaxStepSize());
307315
this->dataPtr->components[_Id]->SetAttributeValue("realTimeFactor",
@@ -311,6 +319,7 @@ void Plotting::SetData(std::string _Id, const sdf::Physics &_physics)
311319
//////////////////////////////////////////////////
312320
void Plotting::SetData(std::string _Id, const double &_value)
313321
{
322+
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->componentsMutex);
314323
this->dataPtr->components[_Id]->SetAttributeValue("value", _value);
315324
}
316325

@@ -321,6 +330,7 @@ void Plotting::RegisterChartToComponent(uint64_t _entity, uint64_t _typeId,
321330
std::string _attribute,
322331
int _chart)
323332
{
333+
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->componentsMutex);
324334
std::string Id = std::to_string(_entity) + "," + std::to_string(_typeId);
325335

326336
if (this->dataPtr->components.count(Id) == 0)
@@ -336,6 +346,7 @@ void Plotting::RegisterChartToComponent(uint64_t _entity, uint64_t _typeId,
336346
void Plotting::UnRegisterChartFromComponent(uint64_t _entity, uint64_t _typeId,
337347
std::string _attribute, int _chart)
338348
{
349+
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->componentsMutex);
339350
std::string id = std::to_string(_entity) + "," + std::to_string(_typeId);
340351
igndbg << "UnRegister [" << id << "]" << std::endl;
341352

@@ -345,7 +356,7 @@ void Plotting::UnRegisterChartFromComponent(uint64_t _entity, uint64_t _typeId,
345356
this->dataPtr->components[id]->UnRegisterChart(_attribute, _chart);
346357

347358
if (!this->dataPtr->components[id]->HasCharts())
348-
this->dataPtr->components.erase(id);
359+
this->dataPtr->components.erase(id);
349360
}
350361

351362
//////////////////////////////////////////////////
@@ -362,9 +373,10 @@ std::string Plotting::ComponentName(const uint64_t &_typeId)
362373
}
363374

364375
//////////////////////////////////////////////////
365-
void Plotting ::Update(const ignition::gazebo::UpdateInfo &_info,
376+
void Plotting::Update(const ignition::gazebo::UpdateInfo &_info,
366377
ignition::gazebo::EntityComponentManager &_ecm)
367378
{
379+
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->componentsMutex);
368380
for (auto component : this->dataPtr->components)
369381
{
370382
auto entity = component.second->Entity();
@@ -509,6 +521,6 @@ void Plotting ::Update(const ignition::gazebo::UpdateInfo &_info,
509521
}
510522

511523
// Register this plugin
512-
IGNITION_ADD_PLUGIN(ignition::gazebo::Plotting ,
524+
IGNITION_ADD_PLUGIN(ignition::gazebo::Plotting,
513525
ignition::gazebo::GuiSystem,
514526
ignition::gui::Plugin)

0 commit comments

Comments
 (0)