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

View File

@ -51,7 +51,7 @@ namespace detail {
/// them alive the whole run.
std::unordered_map<
stream_id_type,
std::shared_ptr<StreamStateBase>> _stream_map;
std::weak_ptr<StreamStateBase>> _stream_map;
};
} // 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
#include "GameFramework/Actor.h"
#include "Carla/Actor/ActorDescription.h"
#include "Carla/Actor/ActorBlueprintFunctionLibrary.h"
#include "Carla/Sensor/DataStream.h"
#include "GameFramework/Actor.h"
#include "Sensor.generated.h"
struct FActorDescription;
/// Base class for sensors.
UCLASS(Abstract, hidecategories = (Collision, Attachment, Actor))
class CARLA_API ASensor : public AActor
@ -22,16 +22,7 @@ class CARLA_API ASensor : public AActor
public:
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));
}
}
virtual void Set(const FActorDescription &Description);
/// Replace the FDataStream associated with this sensor.
///
@ -43,6 +34,8 @@ public:
protected:
void EndPlay(EEndPlayReason::Type EndPlayReason) override;
/// Return the FDataStream associated with this sensor.
FDataStream &GetDataStream()
{

View File

@ -161,6 +161,10 @@ private:
return it->second;
}
void ClearSensorStream(FActorView ActorView) {
_StreamMap.erase(ActorView.GetActorId());
}
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"));
return false;
}
ClearSensorStream(ActorView);
return Episode->DestroyActor(ActorView.GetActor());
});