From 00d9697b9dbc2ee09aa08807bdf490716a8e1ea9 Mon Sep 17 00:00:00 2001 From: falsycat Date: Tue, 15 Nov 2022 15:41:48 +0900 Subject: [PATCH] add leak checker for nf7::Context --- main.cc | 14 ++++++++++++++ nf7.cc | 2 ++ nf7.hh | 4 ++++ 3 files changed, 20 insertions(+) diff --git a/main.cc b/main.cc index f55527b..29d152c 100644 --- a/main.cc +++ b/main.cc @@ -170,6 +170,11 @@ class Env final : public nf7::Env { } } } + ~Env() noexcept { + if (ctx_cnt_ > 0) { + std::cout << "context leak detected: " << ctx_cnt_ << std::endl; + } + } void TearDownRoot() noexcept { if (root_) { @@ -270,6 +275,13 @@ class Env final : public nf7::Env { files_.erase(id); } + void AddContext(nf7::Context&) noexcept override { + ++ctx_cnt_; + } + void RemoveContext(nf7::Context&) noexcept override { + --ctx_cnt_; + } + void AddWatcher(nf7::File::Id id, nf7::Env::Watcher& w) noexcept override { watchers_[id].push_back(&w); } @@ -293,6 +305,8 @@ class Env final : public nf7::Env { std::unordered_map files_; std::unordered_map> watchers_; + + std::atomic ctx_cnt_ = 0; }; diff --git a/nf7.cc b/nf7.cc index 75fc5f8..ea4a549 100644 --- a/nf7.cc +++ b/nf7.cc @@ -254,8 +254,10 @@ Context::Context(File& f, const std::shared_ptr& parent) noexcept : Context::Context(Env& env, File::Id initiator, const std::shared_ptr& parent) noexcept : env_(&env), initiator_(initiator), parent_(parent), depth_(parent? parent->depth()+1: 0) { + env_->AddContext(*this); } Context::~Context() noexcept { + env_->RemoveContext(*this); } File& Env::GetFileOrThrow(File::Id id) const { diff --git a/nf7.hh b/nf7.hh index e608117..083b241 100644 --- a/nf7.hh +++ b/nf7.hh @@ -301,6 +301,10 @@ class Env { virtual File::Id AddFile(File&) noexcept = 0; virtual void RemoveFile(File::Id) noexcept = 0; + friend class nf7::Context; + virtual void AddContext(nf7::Context&) noexcept = 0; + virtual void RemoveContext(nf7::Context&) noexcept = 0; + virtual void AddWatcher(File::Id, Watcher&) noexcept = 0; virtual void RemoveWatcher(File::Id, Watcher&) noexcept = 0;