diff --git a/paddle/fluid/lite/core/memory.h b/paddle/fluid/lite/core/memory.h index 5948f6c4a854d9f678c316f351c017788c44c4a2..6b019abc19d4e0e0add32b23d3f39820b8b47588 100644 --- a/paddle/fluid/lite/core/memory.h +++ b/paddle/fluid/lite/core/memory.h @@ -65,6 +65,8 @@ class Buffer { TargetCopy(target_, data_, other.data_, nbytes); } + ~Buffer() { Free(); } + private: // memory it actually malloced. size_t space_{0}; diff --git a/paddle/fluid/lite/utils/any.h b/paddle/fluid/lite/utils/any.h index 466deae3de92ad5992695a505108e1e31b68a826..33e697a6da566543fbe1d0a44e0f7b8ac25239d8 100644 --- a/paddle/fluid/lite/utils/any.h +++ b/paddle/fluid/lite/utils/any.h @@ -34,7 +34,6 @@ class Any { CHECK(type_ == typeid(T).hash_code()); } else { type_ = typeid(T).hash_code(); - data_ = new T; deleter_ = [&] { delete static_cast(data_); }; } data_ = new T; @@ -55,10 +54,16 @@ class Any { bool valid() const { return data_; } + ~Any() { + if (valid()) { + deleter_(); + } + } + private: static size_t kInvalidType; size_t type_{kInvalidType}; - void* data_{}; + void* data_{nullptr}; std::function deleter_; };