From 52db636c108ab0a16ba0ccf8df55cf28142a230c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Fri, 19 Feb 2021 22:17:22 +0100 Subject: Storage: keep weak pointer to Storage in head watcher --- src/storage.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/storage.cpp') diff --git a/src/storage.cpp b/src/storage.cpp index f50b471..bd0beae 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -37,6 +37,7 @@ using std::shared_ptr; using std::string; using std::system_error; using std::to_string; +using std::weak_ptr; FilesystemStorage::FilesystemStorage(const fs::path & path): root(path) @@ -832,10 +833,11 @@ optional Storage::updateHead(UUID type, UUID id, const Ref & old, const std int Storage::watchHead(UUID type, UUID wid, const std::function watcher) const { - return p->backend->watchHead(type, [this, wid, watcher] (UUID id, const Digest & dgst) { + return p->backend->watchHead(type, [wp = weak_ptr(p), wid, watcher] (UUID id, const Digest & dgst) { if (id == wid) - if (auto r = ref(dgst)) - watcher(*r); + if (auto p = wp.lock()) + if (auto r = Ref::create(Storage(p), dgst)) + watcher(*r); }); } @@ -889,7 +891,7 @@ Digest Digest::of(const vector & content) } -PartialRef PartialRef::create(PartialStorage st, const Digest & digest) +PartialRef PartialRef::create(const PartialStorage & st, const Digest & digest) { auto p = new Priv { .storage = make_unique(st), @@ -926,7 +928,7 @@ const PartialStorage & PartialRef::storage() const return *p->storage; } -optional Ref::create(Storage st, const Digest & digest) +optional Ref::create(const Storage & st, const Digest & digest) { if (!st.p->backend->contains(digest)) return nullopt; @@ -939,7 +941,7 @@ optional Ref::create(Storage st, const Digest & digest) return Ref(shared_ptr(p)); } -Ref Ref::zcreate(Storage st) +Ref Ref::zcreate(const Storage & st) { auto p = new Priv { .storage = make_unique(st), -- cgit v1.2.3