智能指针-weak_ptr的用法和实现

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


1.shared_ptr的缺点

如果std::shared_ptr仔细考虑,您仍然会发现仍然存在无法释放资源的问题。看下面的例子

#include <mcheck.h>  //内存泄露检测工具
#include <iostream>
#include <memory>
class A;
class B;

class A{
   public:
    std::shared_ptr<B> pointer;
    ~A() {
        std::cout << "A was destroyed" << std::endl;
    }
};
class B{
   public:
    std::shared_ptr<A> pointer;
    ~B() {
        std::cout << "B was destroyed" << std::endl;
    }
};
int main() {
    setenv("MALLOC_TRACE", "m.log", 1);
    mtrace();
    std::shared_ptr<A> a = std::make_shared<A>();
    std::shared_ptr<B> b = std::make_shared<B>();
    a->pointer = b;
    b->pointer = a;
    return 0;
}

Memory not freed:
Address Size Caller
0x0000000000b84370 0x20 at 0x7f6e26ecee78
0x0000000000b843a0 0x20 at 0x7f6e26ecee78

原因:这两块内存均由A(B)指针和B(A)的成员指针所指向,A和B析构后,堆上成员指针和内存依然相互依存,造成内存泄露。
解决方法:使用弱引用weak_ptr,弱引用不会引起计数器计数。

2.简单实现

代码如下(示例):

#include <iostream>
using namespace std;
template <typename T>
class weak_ptr;
class Count {
   public:
    int s;
    int w;
    Count() : s(0), w(0){};
    ~Count(){};
};
template <typename T>
class smartptr {
   public:
   //类初始化
    smartptr(T* p = nullptr) : ptr_(p) {
        cnt_ = new Count();
        if (p)
            cnt_->s = 1;
    }
    ~smartptr() {
        del();
    }
    //smartptr拷贝构造函数 count.s++
    smartptr(const smartptr<T>& orig) : ptr_(orig.ptr_), cnt_(orig.cnt_) {
        cnt_->s++;
    }
    smartptr<T> operator=(const smartptr<T>& orig) {
        if (this != &orig) {
            del();
            ptr_ = orig.ptr_;
            cnt_ = orig.cnt_;
            cnt_->s++;
        }
        return orig;
    }

    int use_count() {
        return cnt_->s;
    }
    T* operator->() {
        return ptr_;
    }
    T& operator*() {
        return *ptr_;
    }
    T* get() {
        return ptr_;
    }

    friend class weak_ptr<T>;

   private:
    T* ptr_;
    //堆上方便共享
    Count* cnt_;
    void del() {
        cnt_->s--;
        if (cnt_->s == 0) {
            delete ptr_;
            ptr_ = nullptr;
            if (cnt_->w == 0) {
                delete cnt_;
                cnt_ = nullptr;
            }
        }
    }
};
template <typename T>
class weak_ptr {
   public:
    weak_ptr(smartptr<T>& s) : ptr_(s.ptr_), cnt_(s.cnt_) {
        if (ptr_ != s.ptr_)
            cnt_->w++;
    }
    weak_ptr(weak_ptr<T>& s) : ptr_(s.ptr_), cnt_(s.cnt_) {
        if (ptr_ != s.ptr_)
            cnt_->w++;
    }
    weak_ptr<T>& operator=(smartptr<T>& s){
        if(ptr_!=s.ptr_){
            del();
            cnt_ = s.cnt_;
            cnt_->w++;
        }
        return *this;
    }
    weak_ptr<T>& operator=(weak_ptr<T>& w){
        if(ptr_!=w.ptr_){
            del();
            cnt_ = w.cnt_;
            cnt_->w++;
        }
        return w;
    }    
    ~weak_ptr() {
        del();
    }
   private:
    T* ptr_;
    Count* cnt_;
    void del() {
        if (cnt_) {
            cnt_->w--;
            if (cnt_->w == 0 && cnt_->s == 0)
                cnt_ = nullptr;
        }
    }
};

int main() {
    smartptr<int> p1(new int(0));
    cout << p1.use_count() << endl;
    smartptr<int> p2(p1);
    cout << p2.use_count() << endl;
    smartptr<int> p3 = p1;
    cout << p3.use_count() << endl;
    *p1.get() = 10;
    cout << "p1: " << *p1 << endl;
    weak_ptr<int> p4(p1);
    cout << p3.use_count() << endl;

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Attention is all you

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值