Garbage collect unused radar product manager instances

This commit is contained in:
Dan Paulat 2022-10-09 00:27:54 -05:00
parent 384cfb53ed
commit 36eaea466e
2 changed files with 29 additions and 15 deletions

View file

@ -50,8 +50,7 @@ static const std::string kDefaultLevel3Product_ {"N0B"};
static constexpr std::chrono::seconds kRetryInterval_ {15};
// TODO: Find a way to garbage collect this
static std::unordered_map<std::string, std::shared_ptr<RadarProductManager>>
static std::unordered_map<std::string, std::weak_ptr<RadarProductManager>>
instanceMap_;
static std::mutex instanceMutex_;
@ -960,14 +959,22 @@ RadarProductManager::Instance(const std::string& radarSite)
{
std::lock_guard<std::mutex> guard(instanceMutex_);
if (!instanceMap_.contains(radarSite))
// Look up instance weak pointer
auto it = instanceMap_.find(radarSite);
if (it != instanceMap_.end())
{
instanceMap_[radarSite] =
std::make_shared<RadarProductManager>(radarSite);
instanceCreated = true;
// Attempt to convert the weak pointer to a shared pointer. It may have
// been garbage collected.
instance = it->second.lock();
}
instance = instanceMap_[radarSite];
// If no active instance was found, create a new one
if (instance == nullptr)
{
instance = std::make_shared<RadarProductManager>(radarSite);
instanceMap_.insert_or_assign(radarSite, instance);
instanceCreated = true;
}
}
if (instanceCreated)

View file

@ -50,17 +50,24 @@ RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) :
{
logger_->debug("Adding radar site: {}", radarSite);
const QModelIndex rootIndex =
self_->createIndex(rootItem_->row(), 0, rootItem_.get());
const int rootChildren = rootItem_->child_count();
const QString radarSiteName {QString::fromStdString(radarSite)};
self_->beginInsertRows(rootIndex, rootChildren, rootChildren);
// Find existing radar site item (e.g., KLSX, KEAX)
TreeItem* radarSiteItem = rootItem_->FindChild(0, radarSiteName);
TreeItem* radarSiteItem =
new TreeItem({QString::fromStdString(radarSite)});
rootItem_->AppendChild(radarSiteItem);
if (radarSiteItem == nullptr)
{
const QModelIndex rootIndex =
self_->createIndex(rootItem_->row(), 0, rootItem_.get());
const int rootChildren = rootItem_->child_count();
self_->endInsertRows();
self_->beginInsertRows(rootIndex, rootChildren, rootChildren);
radarSiteItem = new TreeItem({radarSiteName});
rootItem_->AppendChild(radarSiteItem);
self_->endInsertRows();
}
connect(
manager::RadarProductManager::Instance(radarSite).get(),