diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 4410bce1..10a7f3c5 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -3833,39 +3833,60 @@ void FileManager::memory_cleanup() { } /* DESTROY INVALID file_hash_to_file_id_ */ - for (const auto &pair : file_hash_to_file_id_) { - auto &file = file_id_info_[pair.second.fast_get()]; - if (&file == nullptr || file_nodes_[file.node_id_] == nullptr) { - file_hash_to_file_id_.erase(pair.first); + { + auto it = file_hash_to_file_id_.begin(); + while (it != file_hash_to_file_id_.end()) { + auto &file = file_id_info_[it->second.fast_get()]; + if (file_nodes_[file.node_id_] == nullptr) { + file_hash_to_file_id_.erase(it); + } else { + it++; + } } } /* DESTROY INVALID local_location_to_file_id_ */ - for (const auto &pair : local_location_to_file_id_) { - auto &file = file_id_info_[pair.second.fast_get()]; - if (&file == nullptr || file_nodes_[file.node_id_] == nullptr) { - local_location_to_file_id_.erase(pair.first); + { + auto it = local_location_to_file_id_.begin(); + while (it != local_location_to_file_id_.end()) { + auto &file = file_id_info_[it->second.fast_get()]; + if (file_nodes_[file.node_id_] == nullptr) { + it = local_location_to_file_id_.erase(it); + } else { + it++; + } } } /* DESTROY INVALID generate_location_to_file_id_ */ - for (const auto &pair : generate_location_to_file_id_) { - auto &file = file_id_info_[pair.second.fast_get()]; - if (&file == nullptr || file_nodes_[file.node_id_] == nullptr) { - generate_location_to_file_id_.erase(pair.first); + { + auto it = generate_location_to_file_id_.begin(); + while (it != generate_location_to_file_id_.end()) { + auto &file = file_id_info_[it->second.fast_get()]; + if (file_nodes_[file.node_id_] == nullptr) { + it = generate_location_to_file_id_.erase(it); + } else { + it++; + } } } /* DESTROY INVALID remote_location_info_ */ - for (const auto &pair : remote_location_info_.get_map()) { - auto &file = file_id_info_[pair.first.file_id_.fast_get()]; - if (&file == nullptr || file_nodes_[file.node_id_] == nullptr) { - remote_location_info_.erase_map_key(pair.first, pair.second); + { + auto map = remote_location_info_.get_map(); + auto it = map.begin(); + while (it != map.end()) { + auto &file = file_id_info_[it->first.file_id_.fast_get()]; + if (file_nodes_[file.node_id_] == nullptr) { + map.erase(it); + remote_location_info_.erase(it->second); + } else { + it++; + } } } file_hash_to_file_id_.rehash(0); - file_id_info_.shrink_to_fit(); empty_file_ids_.shrink_to_fit(); empty_node_ids_.shrink_to_fit(); diff --git a/tdutils/td/utils/Enumerator.h b/tdutils/td/utils/Enumerator.h index b22107e7..3cc1f4af 100644 --- a/tdutils/td/utils/Enumerator.h +++ b/tdutils/td/utils/Enumerator.h @@ -19,8 +19,8 @@ class Enumerator { public: using Key = int32; - std::map get_map() const { - return map_; + std::map &get_map() const { + return *map_; } std::pair next() { @@ -33,10 +33,9 @@ class Enumerator { return std::make_pair((Key) (arr_.size() + 1), false); } - void erase_map_key(ValueT key_x, Key key_y) { + void erase(Key key_y) { auto pos_y = static_cast(key_y - 1); empty_id_.push_back(key_y); - map_.erase(key_x); arr_[pos_y] = nullptr; }