mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 17:40:05 +00:00 
			
		
		
		
	Garbage collect unused radar product manager instances
This commit is contained in:
		
							parent
							
								
									384cfb53ed
								
							
						
					
					
						commit
						36eaea466e
					
				
					 2 changed files with 29 additions and 15 deletions
				
			
		|  | @ -50,8 +50,7 @@ static const std::string kDefaultLevel3Product_ {"N0B"}; | ||||||
| 
 | 
 | ||||||
| static constexpr std::chrono::seconds kRetryInterval_ {15}; | static constexpr std::chrono::seconds kRetryInterval_ {15}; | ||||||
| 
 | 
 | ||||||
| // TODO: Find a way to garbage collect this
 | static std::unordered_map<std::string, std::weak_ptr<RadarProductManager>> | ||||||
| static std::unordered_map<std::string, std::shared_ptr<RadarProductManager>> |  | ||||||
|                   instanceMap_; |                   instanceMap_; | ||||||
| static std::mutex instanceMutex_; | static std::mutex instanceMutex_; | ||||||
| 
 | 
 | ||||||
|  | @ -960,14 +959,22 @@ RadarProductManager::Instance(const std::string& radarSite) | ||||||
|    { |    { | ||||||
|       std::lock_guard<std::mutex> guard(instanceMutex_); |       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] = |          // Attempt to convert the weak pointer to a shared pointer. It may have
 | ||||||
|             std::make_shared<RadarProductManager>(radarSite); |          // been garbage collected.
 | ||||||
|          instanceCreated = true; |          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) |    if (instanceCreated) | ||||||
|  |  | ||||||
|  | @ -50,17 +50,24 @@ RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) : | ||||||
|       { |       { | ||||||
|          logger_->debug("Adding radar site: {}", radarSite); |          logger_->debug("Adding radar site: {}", radarSite); | ||||||
| 
 | 
 | ||||||
|  |          const QString radarSiteName {QString::fromStdString(radarSite)}; | ||||||
|  | 
 | ||||||
|  |          // Find existing radar site item (e.g., KLSX, KEAX)
 | ||||||
|  |          TreeItem* radarSiteItem = rootItem_->FindChild(0, radarSiteName); | ||||||
|  | 
 | ||||||
|  |          if (radarSiteItem == nullptr) | ||||||
|  |          { | ||||||
|             const QModelIndex rootIndex = |             const QModelIndex rootIndex = | ||||||
|                self_->createIndex(rootItem_->row(), 0, rootItem_.get()); |                self_->createIndex(rootItem_->row(), 0, rootItem_.get()); | ||||||
|             const int rootChildren = rootItem_->child_count(); |             const int rootChildren = rootItem_->child_count(); | ||||||
| 
 | 
 | ||||||
|             self_->beginInsertRows(rootIndex, rootChildren, rootChildren); |             self_->beginInsertRows(rootIndex, rootChildren, rootChildren); | ||||||
| 
 | 
 | ||||||
|          TreeItem* radarSiteItem = |             radarSiteItem = new TreeItem({radarSiteName}); | ||||||
|             new TreeItem({QString::fromStdString(radarSite)}); |  | ||||||
|             rootItem_->AppendChild(radarSiteItem); |             rootItem_->AppendChild(radarSiteItem); | ||||||
| 
 | 
 | ||||||
|             self_->endInsertRows(); |             self_->endInsertRows(); | ||||||
|  |          } | ||||||
| 
 | 
 | ||||||
|          connect( |          connect( | ||||||
|             manager::RadarProductManager::Instance(radarSite).get(), |             manager::RadarProductManager::Instance(radarSite).get(), | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat