Skip to content

Commit 883a0ab

Browse files
committed
Fix crash and revert pose-publisher changes.
Thanks @azeey I found the source of the crash. It comes from the fact that the PostUpdate system list changes with time. The reference taken by the thread to the PostUpdate interface will change over time. Thus we copy the interface pointer. Signed-off-by: Arjo Chakravarty <arjoc@google.com>
1 parent f3c57d0 commit 883a0ab

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/SimulationRunner.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ void SimulationRunner::ProcessSystemQueue()
559559
this->postUpdateThreads.push_back(std::thread([&, id]()
560560
{
561561
auto parentEntity = system.parent;
562+
auto this_thread_system = system.system;
562563
std::stringstream ss;
563564
ss << "PostUpdateThread: " << id;
564565
GZ_PROFILE_THREAD_NAME(ss.str().c_str());
@@ -574,9 +575,10 @@ void SimulationRunner::ProcessSystemQueue()
574575
this->postUpdateStopBarrier->Drop();
575576
break;
576577
}
577-
system.system->PostUpdate(this->currentInfo, this->entityCompMgr);
578+
this_thread_system->PostUpdate(this->currentInfo, this->entityCompMgr);
578579
}
579580
this->postUpdateStopBarrier->Wait();
581+
580582
}
581583

582584
gzdbg << "Exiting postupdate worker thread ("

src/systems/pose_publisher/PosePublisher.cc

+22-12
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ class gz::sim::systems::PosePublisherPrivate
155155
/// improves performance by avoiding memory allocation
156156
public: std::vector<std::pair<Entity, math::Pose3d>> staticPoses;
157157

158+
/// \brief A variable that gets populated with poses. This also here as a
159+
/// member variable to avoid repeated memory allocations and improve
160+
/// performance.
161+
public: msgs::Pose poseMsg;
162+
163+
/// \brief A variable that gets populated with poses. This also here as a
164+
/// member variable to avoid repeated memory allocations and improve
165+
/// performance.
166+
public: msgs::Pose_V poseVMsg;
167+
158168
/// \brief True to publish a vector of poses. False to publish individual pose
159169
/// msgs.
160170
public: bool usePoseV = false;
@@ -242,8 +252,7 @@ void PosePublisher::Configure(const Entity &_entity,
242252
this->dataPtr->usePoseV =
243253
_sdf->Get<bool>("use_pose_vector_msg", this->dataPtr->usePoseV).first;
244254

245-
std::string poseTopic = scopedName(_entity, _ecm) + "/pose";
246-
poseTopic = transport::TopicUtils::AsValidTopic(poseTopic);
255+
std::string poseTopic = topicFromScopedName(_entity, _ecm, false) + "/pose";
247256
if (poseTopic.empty())
248257
{
249258
poseTopic = "/pose";
@@ -287,8 +296,8 @@ void PosePublisher::PostUpdate(const UpdateInfo &_info,
287296
if (_info.dt < std::chrono::steady_clock::duration::zero())
288297
{
289298
gzwarn << "Detected jump back in time ["
290-
<< std::chrono::duration_cast<std::chrono::seconds>(_info.dt).count()
291-
<< "s]. System may not work properly." << std::endl;
299+
<< std::chrono::duration<double>(_info.dt).count()
300+
<< "s]. System may not work properly." << std::endl;
292301
}
293302

294303
// Nothing left to do if paused.
@@ -520,10 +529,11 @@ void PosePublisherPrivate::PublishPoses(
520529
transport::Node::Publisher &_publisher)
521530
{
522531
GZ_PROFILE("PosePublisher::PublishPoses");
523-
msgs::Pose poseMsg;
524-
msgs::Pose_V poseVMsg;
532+
525533
// publish poses
526534
msgs::Pose *msg = nullptr;
535+
if (this->usePoseV)
536+
this->poseVMsg.Clear();
527537

528538
for (const auto &[entity, pose] : _poses)
529539
{
@@ -533,12 +543,12 @@ void PosePublisherPrivate::PublishPoses(
533543

534544
if (this->usePoseV)
535545
{
536-
msg = poseVMsg.add_pose();
546+
msg = this->poseVMsg.add_pose();
537547
}
538548
else
539549
{
540-
poseMsg.Clear();
541-
msg = &poseMsg;
550+
this->poseMsg.Clear();
551+
msg = &this->poseMsg;
542552
}
543553

544554
// fill pose msg
@@ -565,12 +575,12 @@ void PosePublisherPrivate::PublishPoses(
565575

566576
// publish individual pose msgs
567577
if (!this->usePoseV)
568-
_publisher.Publish(poseMsg);
578+
_publisher.Publish(this->poseMsg);
569579
}
570580

571581
// publish pose vector msg
572582
if (this->usePoseV)
573-
_publisher.Publish(poseVMsg);
583+
_publisher.Publish(this->poseVMsg);
574584
}
575585

576586
GZ_ADD_PLUGIN(PosePublisher,
@@ -579,4 +589,4 @@ GZ_ADD_PLUGIN(PosePublisher,
579589
PosePublisher::ISystemPostUpdate)
580590

581591
GZ_ADD_PLUGIN_ALIAS(PosePublisher,
582-
"gz::sim::systems::PosePublisher")
592+
"gz::sim::systems::PosePublisher") //14,16

0 commit comments

Comments
 (0)