fix memcheck leak issue (#4308)

Signed-off-by: peng.xu <peng.xu@zilliz.com>
Signed-off-by: shengjun.li <shengjun.li@zilliz.com>
pull/4348/head
XuPeng-SH 2020-11-27 20:15:19 +08:00 committed by shengjun.li
parent 178289e0bb
commit 8341138404
3 changed files with 21 additions and 17 deletions

View File

@ -18,10 +18,10 @@
namespace milvus::engine::snapshot { namespace milvus::engine::snapshot {
using OnNoRefCBF = std::function<void(void)>; class ReferenceProxy : public std::enable_shared_from_this<ReferenceProxy> {
class ReferenceProxy {
public: public:
using Ptr = std::shared_ptr<ReferenceProxy>;
using OnNoRefCBF = std::function<void(Ptr)>;
ReferenceProxy() = default; ReferenceProxy() = default;
virtual ~ReferenceProxy() = default; virtual ~ReferenceProxy() = default;
@ -42,7 +42,7 @@ class ReferenceProxy {
} }
if (ref_count_.fetch_sub(1) == 1) { if (ref_count_.fetch_sub(1) == 1) {
for (auto& cb : on_no_ref_cbs_) { for (auto& cb : on_no_ref_cbs_) {
cb(); cb(this->shared_from_this());
} }
} }
} }

View File

@ -123,7 +123,7 @@ class ResourceHolder {
return false; return false;
} }
id_map_[resource->GetID()] = resource; id_map_[resource->GetID()] = resource;
resource->RegisterOnNoRefCB(std::bind(&Derived::OnNoRefCallBack, this, resource)); resource->RegisterOnNoRefCB(std::bind(&Derived::OnNoRefCallBack, this, std::placeholders::_1));
return true; return true;
} }
@ -138,10 +138,14 @@ class ResourceHolder {
} }
virtual void virtual void
OnNoRefCallBack(ResourcePtr resource) { OnNoRefCallBack(ReferenceProxy::Ptr resource) {
resource->Deactivate(); auto res = std::dynamic_pointer_cast<ResourceT>(resource);
Release(resource->GetID()); if (!res) {
auto evt_ptr = std::make_shared<ResourceGCEvent<ResourceT>>(resource); return;
}
res->Deactivate();
Release(res->GetID());
auto evt_ptr = std::make_shared<ResourceGCEvent<ResourceT>>(res);
EventExecutor::GetInstance().Submit(evt_ptr); EventExecutor::GetInstance().Submit(evt_ptr);
} }

View File

@ -44,25 +44,25 @@ TEST_F(SnapshotTest, ResourcesTest) {
TEST_F(SnapshotTest, ReferenceProxyTest) { TEST_F(SnapshotTest, ReferenceProxyTest) {
std::string status("raw"); std::string status("raw");
const std::string CALLED = "CALLED"; const std::string CALLED = "CALLED";
auto callback = [&]() { auto callback = [&](ReferenceProxy::Ptr) {
status = CALLED; status = CALLED;
}; };
auto proxy = ReferenceProxy(); auto proxy = std::make_shared<ReferenceProxy>();
ASSERT_EQ(proxy.ref_count(), 0); ASSERT_EQ(proxy->ref_count(), 0);
int refcnt = 3; int refcnt = 3;
for (auto i = 0; i < refcnt; ++i) { for (auto i = 0; i < refcnt; ++i) {
proxy.Ref(); proxy->Ref();
} }
ASSERT_EQ(proxy.ref_count(), refcnt); ASSERT_EQ(proxy->ref_count(), refcnt);
proxy.RegisterOnNoRefCB(callback); proxy->RegisterOnNoRefCB(callback);
for (auto i = 0; i < refcnt; ++i) { for (auto i = 0; i < refcnt; ++i) {
proxy.UnRef(); proxy->UnRef();
} }
ASSERT_EQ(proxy.ref_count(), 0); ASSERT_EQ(proxy->ref_count(), 0);
ASSERT_EQ(status, CALLED); ASSERT_EQ(status, CALLED);
} }