Skip to content

Commit 3c352cd

Browse files
committed
+ Fix for crash during detach
pal-robotics#11
1 parent 88e6f06 commit 3c352cd

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

include/gazebo_ros_link_attacher.h

+4
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ namespace gazebo
7070
gazebo_ros_link_attacher::Attach::Response &res);
7171
bool detach_callback(gazebo_ros_link_attacher::Attach::Request &req,
7272
gazebo_ros_link_attacher::Attach::Response &res);
73+
void OnUpdate();
7374

7475
std::vector<fixedJoint> joints;
76+
std::vector<fixedJoint> detach_vector;
77+
78+
event::ConnectionPtr beforePhysicsUpdateConnection;
7579

7680
/// \brief The physics engine.
7781
physics::PhysicsEnginePtr physics;

src/gazebo_ros_link_attacher.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ namespace gazebo
1515
GazeboRosLinkAttacher::GazeboRosLinkAttacher() :
1616
nh_("link_attacher_node")
1717
{
18+
std::vector<fixedJoint> vect;
19+
this->detach_vector = vect;
20+
this->beforePhysicsUpdateConnection =
21+
event::Events::ConnectBeforePhysicsUpdate(std::bind(&GazeboRosLinkAttacher::OnUpdate, this));
1822
}
1923

2024

@@ -172,7 +176,8 @@ namespace gazebo
172176
// search for the instance of joint and do detach
173177
fixedJoint j;
174178
if(this->getJoint(model1, link1, model2, link2, j)){
175-
j.joint->Detach();
179+
this->detach_vector.push_back(j);
180+
ROS_INFO_STREAM("Detach joint request pushed in the detach vector");
176181
return true;
177182
}
178183

@@ -231,4 +236,24 @@ namespace gazebo
231236
return true;
232237
}
233238

239+
// thanks to https://answers.gazebosim.org/question/12118/intermittent-segmentation-fault-possibly-by-custom-worldplugin-attaching-and-detaching-child/?answer=24271#post-id-24271
240+
void GazeboRosLinkAttacher::OnUpdate()
241+
{
242+
if(!this->detach_vector.empty())
243+
{
244+
ROS_INFO_STREAM("Received before physics update callback... Detaching joints");
245+
std::vector<fixedJoint>::iterator it;
246+
it = this->detach_vector.begin();
247+
fixedJoint j;
248+
while (it != this->detach_vector.end())
249+
{
250+
j = *it;
251+
j.joint->Detach();
252+
ROS_INFO_STREAM("Joint detached !");
253+
++it;
254+
}
255+
detach_vector.clear();
256+
}
257+
}
258+
234259
}

0 commit comments

Comments
 (0)