Skip to content

Commit 3f62db3

Browse files
committed
fix(ros2): move ROS2 unregistration before actor deregistration
Move ROS2 UnregisterSensor/UnregisterVehicle calls inside the CarlaActor null check, before Registry.Deregister. This prevents a potential null pointer dereference in multi-GPU setups where CarlaActor may be null when OnActorDestroyed fires. Also add "actor" prefix to auto-generated ROS names for clarity. Port of ue4-dev commit 201d375. Signed-off-by: Yutaka Kondo <yutaka.kondo@youtalk.jp>
1 parent fee14f4 commit 3f62db3

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Actor/ActorDispatcher.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ FCarlaActor* UActorDispatcher::RegisterActor(
180180
// If not specified by the user, set the ActorId as the actor name
181181
if (RosName.empty())
182182
{
183-
RosName = std::to_string(View->GetActorId());
183+
RosName = "actor" + std::to_string(View->GetActorId());
184184
}
185185

186186
std::string FrameId = std::string(TCHAR_TO_UTF8(*Description.GetAttribute("ros_frame_id").Value));
@@ -231,26 +231,27 @@ void UActorDispatcher::OnActorDestroyed(AActor *Actor)
231231
FCarlaActor* CarlaActor = Registry.FindCarlaActor(Actor);
232232
if (CarlaActor)
233233
{
234-
if (CarlaActor->IsActive())
234+
#if defined(WITH_ROS2)
235+
auto ROS2 = carla::ros2::ROS2::GetInstance();
236+
if (ROS2->IsEnabled())
235237
{
236-
Registry.Deregister(CarlaActor->GetActorId());
237-
}
238-
}
238+
auto Description = CarlaActor->GetActorInfo()->Description;
239239

240-
#if defined(WITH_ROS2)
241-
auto ROS2 = carla::ros2::ROS2::GetInstance();
242-
if (ROS2->IsEnabled())
243-
{
244-
auto *Sensor = Cast<ASensor>(Actor);
245-
auto *Vehicle = Cast<ACarlaWheeledVehicle>(Actor);
246-
if (Sensor != nullptr)
247-
{
248-
ROS2->UnregisterSensor(static_cast<void*>(Actor));
240+
auto *Sensor = Cast<ASensor>(Actor);
241+
auto *Vehicle = Cast<ACarlaWheeledVehicle>(Actor);
242+
if (Sensor != nullptr)
243+
{
244+
ROS2->UnregisterSensor(static_cast<void*>(Actor));
245+
}
246+
else if (Vehicle != nullptr && Description.GetAttribute("role_name").Value == "hero") {
247+
ROS2->UnregisterVehicle(static_cast<void*>(Actor));
248+
}
249249
}
250-
else if (Vehicle != nullptr)
250+
#endif
251+
252+
if (CarlaActor->IsActive())
251253
{
252-
ROS2->UnregisterVehicle(static_cast<void*>(Actor));
254+
Registry.Deregister(CarlaActor->GetActorId());
253255
}
254256
}
255-
#endif
256257
}

0 commit comments

Comments
 (0)