#1119 make sure streams are discarded when the sensor is destroyed

This commit is contained in:
nsubiron 2019-01-18 16:46:51 +01:00
parent 159206b298
commit bd398acf28
5 changed files with 58 additions and 24 deletions

View File

@ -32,7 +32,10 @@ namespace detail {
// stopped. // stopped.
for (auto &pair : _stream_map) { for (auto &pair : _stream_map) {
try { try {
pair.second->ClearSessions(); auto stream_state = pair.second.lock();
if (stream_state != nullptr) {
stream_state->ClearSessions();
}
} catch (const std::exception &e) { } catch (const std::exception &e) {
log_error("failed to clear sessions:", e.what()); log_error("failed to clear sessions:", e.what());
} }
@ -56,22 +59,27 @@ namespace detail {
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
auto search = _stream_map.find(session->get_stream_id()); auto search = _stream_map.find(session->get_stream_id());
if (search != _stream_map.end()) { if (search != _stream_map.end()) {
DEBUG_ASSERT(search->second != nullptr); auto stream_state = search->second.lock();
search->second->ConnectSession(std::move(session)); if (stream_state != nullptr) {
stream_state->ConnectSession(std::move(session));
return true; return true;
} else { }
}
log_error("Invalid session: no stream available with id", session->get_stream_id()); log_error("Invalid session: no stream available with id", session->get_stream_id());
return false; return false;
} }
}
void Dispatcher::DeregisterSession(std::shared_ptr<Session> session) { void Dispatcher::DeregisterSession(std::shared_ptr<Session> session) {
DEBUG_ASSERT(session != nullptr); DEBUG_ASSERT(session != nullptr);
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
auto search = _stream_map.find(session->get_stream_id()); auto search = _stream_map.find(session->get_stream_id());
if (search != _stream_map.end()) { if (search != _stream_map.end()) {
DEBUG_ASSERT(search->second != nullptr); auto stream_state = search->second.lock();
search->second->DisconnectSession(session); if (stream_state != nullptr) {
stream_state->DisconnectSession(session);
}
}
}
} }
} }

View File

@ -51,7 +51,7 @@ namespace detail {
/// them alive the whole run. /// them alive the whole run.
std::unordered_map< std::unordered_map<
stream_id_type, stream_id_type,
std::shared_ptr<StreamStateBase>> _stream_map; std::weak_ptr<StreamStateBase>> _stream_map;
}; };
} // namespace detail } // namespace detail

View File

@ -0,0 +1,28 @@
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#include "Carla.h"
#include "Carla/Sensor/Sensor.h"
#include "Carla/Actor/ActorDescription.h"
#include "Carla/Actor/ActorBlueprintFunctionLibrary.h"
void ASensor::Set(const FActorDescription &Description)
{
// set the tick interval of the sensor
if (Description.Variations.Contains("sensor_tick"))
{
SetActorTickInterval(
UActorBlueprintFunctionLibrary::ActorAttributeToFloat(Description.Variations["sensor_tick"],
0.0f));
}
}
void ASensor::EndPlay(EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
Stream = FDataStream();
}

View File

@ -6,14 +6,14 @@
#pragma once #pragma once
#include "GameFramework/Actor.h"
#include "Carla/Actor/ActorDescription.h"
#include "Carla/Actor/ActorBlueprintFunctionLibrary.h"
#include "Carla/Sensor/DataStream.h" #include "Carla/Sensor/DataStream.h"
#include "GameFramework/Actor.h"
#include "Sensor.generated.h" #include "Sensor.generated.h"
struct FActorDescription;
/// Base class for sensors. /// Base class for sensors.
UCLASS(Abstract, hidecategories = (Collision, Attachment, Actor)) UCLASS(Abstract, hidecategories = (Collision, Attachment, Actor))
class CARLA_API ASensor : public AActor class CARLA_API ASensor : public AActor
@ -22,16 +22,7 @@ class CARLA_API ASensor : public AActor
public: public:
virtual void Set(const FActorDescription &Description) virtual void Set(const FActorDescription &Description);
{
// set the tick interval of the sensor
if (Description.Variations.Contains("sensor_tick"))
{
SetActorTickInterval(
UActorBlueprintFunctionLibrary::ActorAttributeToFloat(Description.Variations["sensor_tick"],
0.0f));
}
}
/// Replace the FDataStream associated with this sensor. /// Replace the FDataStream associated with this sensor.
/// ///
@ -43,6 +34,8 @@ public:
protected: protected:
void EndPlay(EEndPlayReason::Type EndPlayReason) override;
/// Return the FDataStream associated with this sensor. /// Return the FDataStream associated with this sensor.
FDataStream &GetDataStream() FDataStream &GetDataStream()
{ {

View File

@ -161,6 +161,10 @@ private:
return it->second; return it->second;
} }
void ClearSensorStream(FActorView ActorView) {
_StreamMap.erase(ActorView.GetActorId());
}
std::unordered_map<FActorView::IdType, carla::streaming::Stream> _StreamMap; std::unordered_map<FActorView::IdType, carla::streaming::Stream> _StreamMap;
}; };
@ -272,6 +276,7 @@ void FTheNewCarlaServer::FPimpl::BindActions()
UE_LOG(LogCarlaServer, Warning, TEXT("unable to destroy actor: not found")); UE_LOG(LogCarlaServer, Warning, TEXT("unable to destroy actor: not found"));
return false; return false;
} }
ClearSensorStream(ActorView);
return Episode->DestroyActor(ActorView.GetActor()); return Episode->DestroyActor(ActorView.GetActor());
}); });