From 2337187e8a229bc573bcefed6695366681aa6f16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Mon, 16 Mar 2015 15:09:41 +0800 Subject: [PATCH] bug fix --- TinySTL/Memory.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/TinySTL/Memory.h b/TinySTL/Memory.h index c34498c..a259797 100644 --- a/TinySTL/Memory.h +++ b/TinySTL/Memory.h @@ -107,20 +107,22 @@ namespace TinySTL{ class shared_ptr{ public: typedef T element_type; + private: + template + using ref_t = Detail::ref_t < Type >; public: explicit shared_ptr(T *p = nullptr) :ref_(new ref_t(p)){} template shared_ptr(T *p, D del) : ref_(new ref_t(p, del)){} shared_ptr(const shared_ptr& sp){ - ref_ = sp.ref_; - ++(*ref_); + copy_ref(sp.ref_); } shared_ptr& operator = (const shared_ptr& sp){ if (this != &sp){ decrease_ref(); - ref_ = sp.ref_; + copy_ref(sp.ref_); } return *this; } @@ -136,16 +138,17 @@ namespace TinySTL{ operator bool() const{ return get() != nullptr; } private: void decrease_ref(){ - if (ref_){ + if (ref_->get_data()){ --(*ref_); if (use_count() == 0) delete ref_; } } + void copy_ref(ref_t *r){//ÕýÈ·µÄ¿½±´ref_t + ref_ = r; + ++(*ref_); + } private: - template - using ref_t = Detail::ref_t < Type > ; - ref_t *ref_; }; template -- GitLab