fix weak_ptr leak

This commit is contained in:
MihailRis
2024-12-12 02:40:59 +03:00
parent 863146cf6b
commit 7e92a16016
4 changed files with 73 additions and 39 deletions
+43
View File
@@ -0,0 +1,43 @@
#include <mutex>
#include <memory>
#include <unordered_map>
namespace util {
template <typename K, typename V>
class WeakPtrsMap {
std::unordered_map<K, std::weak_ptr<V>> map;
std::mutex mutex;
public:
std::weak_ptr<V>& operator[](const K& k) {
return map[k];
}
std::shared_ptr<V> fetch(const K& k) {
auto found = map.find(k);
if (found == map.end()) {
return nullptr;
}
auto ptr = found->second.lock();
if (ptr == nullptr) {
map.erase(found);
}
return ptr;
}
void erase(const K& k) {
map.erase(k);
}
size_t size() const {
return map.size();
}
void lock() {
mutex.lock();
}
void unlock() {
mutex.unlock();
}
};
}