shared_ptr 共享所有权,引用计数
shared_ptr的实现原理是通过引用计数来实现,拷贝或赋值时将引用计数加1,析构时只有当引用计数减到0才释放空间,否则只需将引用计数减1即可.
(ps:shared_ptr在新的C++11标准中叫做unique_ptr)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
template<typename T>class SharedPtr{public: SharedPtr(T* ptr=NULL) :_ptr(ptr) , _refCount(new long(1)) {} ~SharedPtr() { _Release(); } SharedPtr(const SharedPtr<T>& sp) :_ptr(sp._ptr) , _refCount(sp._refCount) { ++(*_refCount); } //传统写法 /*SharedPtr<T>& operator=(const SharedPtr<T>& sp) { if (this != &sp) { this->_Release(); _refCount = sp._refCount; _ptr = sp._ptr; ++(*_refCount); } return *this; }*/ //现代写法 SharedPtr<T>& operator=(SharedPtr<T> sp) { swap(_ptr, sp._ptr); swap(_refCount, sp._refCount); return *this; } T& operator*() { return *_ptr; } T* operator->() { return _ptr; } T* GetPtr() { return _ptr; } long GetCount() { return *_refCount; }protected: void _Release() { if (--(*_refCount) == 0) { //delete _ptr; _del(_ptr); delete _refCount; } }protected: T* _ptr; long* _refCount; //引用计数}; |
为什么引用计数要设置为指针类型,而不设置成整型或静态变量?
请看这篇文章:http://www.linuxidc.com/Linux/2016-05/131825.htm
有时候看似完美的东西,往往都存在着隐含的缺陷,上面的实现仍旧存在问题!
问题如下:
- 在多线程环境下,引用计数的更新存在安全隐患
- 循环引用问题
- 定制删除器
第一个比较容易理解,我们可以在改变引用计数的时候加上一把互斥锁,防止多线程带来的隐患。
第二个,循环引用问题。我们先来看一段利用标准库的shared_ptr 实现的代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include<iostream>using namespace std;#include<memory>struct Node{ shared_ptr<Node> _prev; shared_ptr<Node> _next; ~Node() { cout << "delete :" <<this<< endl; }};void TestSharedPtr(){ shared_ptr<Node> cur(new(Node)); shared_ptr<Node> next(new(Node));} |

cur和next出了作用域,都调用析构函数被释放了。看似正确,但是如果我们加两句代码,问题就出现了:
|
1
2
3
4
5
6
7
|
void TestSharedPtr(){ shared_ptr<Node> cur(new(Node)); shared_ptr<Node> next(new(Node)); cur->_next = next; // 1 next->_prev = cur; // 2} |

我们发现两个节点都没有被释放!我们来分析一下原因:加了这两句代码后,这两个节点的引用计数都增加了1。出了作用域进行析构时,两个对象均不能释放,因为prev的要释放的前提是next释放,而next的释放又依赖于prev的释放。最后就形成了循环引用,谁都释放不了。

解决方案:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
template<class T>struct Node{public: ~Node() { cout << "delete:" << this << endl; }public: weak_ptr<Node> _prev; weak_ptr<Node> _next;}; void TestWeakPtr(){ shared_ptr<Node> cur(new Node()); shared_ptr<Node> next(new Node()); cout << "连接前:" << endl; cout << "cur:" << cur.use_count() << endl; cout << "next:" << next.use_count() << endl; cur->_next = next; next->_prev = cur; cout << "连接后:" << endl; cout << "cur:" << cur.use_count() << endl; cout << "next:" << next.use_count() << endl; } |
因为weak_ptr(弱引用智能指针)会对引用计数会做特殊处理(上述情况不加1)。
第三个,定制删除器。在shared_ptr中只能处理释放new开辟的空间,而对于malloc,以及fopen打开的文件指针不能处理,所以提出了定制删除器,方便释放其他类型的指针,而其实现则是通过仿函数(通过重载operator())来实现。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
template <typename T, class Deleter = Del<T>>class SharedPtr{public: SharedPtr(T* ptr); SharedPtr(T* ptr, Deleter del); SharedPtr(const SharedPtr<T, Deleter>& ap); ~SharedPtr(); //SharedPtr<T, Deleter>& operator=(const SharedPtr<T, Deleter>& ptr);//传统写法 //现代写法 SharedPtr<T, Deleter>& operator=(SharedPtr<T, Deleter> ap); T& operator*()const; T* operator->()const; long GetCount()const; T* GetPtr()const;protected: void _Realease();protected: T* _ptr; long* _pCount; Deleter _del;};template <typename T, class Deleter = Del<T>>SharedPtr<T, Deleter>::SharedPtr(T* ptr) :_ptr(ptr), _pCount(new long(1)){}template <typename T, class Deleter = Del<T>>SharedPtr<T, Deleter>::SharedPtr(T* ptr, Deleter del) : _ptr(ptr), _pCount(new long(1)), _del(del){}template <typename T, class Deleter = Del<T>>SharedPtr<T, Deleter>::SharedPtr(const SharedPtr<T, Deleter>& ap) : _ptr(ap._ptr), _pCount(ap._pCount), _del(ap._del){ ++(*this->_pCount);}template <typename T, class Deleter = Del<T>>SharedPtr<T, Deleter>::~SharedPtr(){ this->_Realease();}//template <typename T, class Deleter = Del<T>>//传统写法//SharedPtr<T, Deleter>& SharedPtr<T, Deleter>::operator=(SharedPtr<T, Deleter> ap)//{// if (this->_ptr != ap._ptr)// {// this->_Realease();// this->_ptr = ap._ptr;// this->_pCount = ap._pCount;// ++(*this->_pCount);// }// return *this;//}template <typename T, class Deleter = Del<T>>//现代写法SharedPtr<T, Deleter>& SharedPtr<T, Deleter>::operator=(SharedPtr<T, Deleter> ap){ swap(this->_ptr, ap._ptr); swap(this->_pCount, ap._pCount); swap(this->_del, ap._del); return *this;}template <typename T, class Deleter = Del<T>>T& SharedPtr<T, Deleter>::operator*()const{ return *(this->_ptr);}template <typename T, class Deleter = Del<T>>T* SharedPtr<T, Deleter>::operator->()const{ return this->_ptr;}template <typename T, class Deleter = Del<T>>long SharedPtr<T, Deleter>::GetCount()const{ return *(this->_pCount);}template <typename T, class Deleter = Del<T>>T* SharedPtr<T, Deleter>::GetPtr()const{ return this->_ptr;}template <typename T, class Deleter = Del<T>>void SharedPtr<T, Deleter>::_Realease(){ if (--(*this->_pCount) == 0) { _del(_ptr); delete this->_pCount; }}template<typename T>struct Free{ void operator()(T* ptr) { cout << "Free:"<<ptr<< endl; free(ptr); }};template<typename T>struct Fclose{ void operator()(T* ptr) { cout << "Fclose:" << ptr << endl; fclose(ptr); }};template<typename T>struct Del{ void operator()(T* ptr) { cout << "Delete:" << ptr << endl; delete(ptr); }};//测试代码void TestDelete(){ int *p1 = (int*)malloc(sizeof(int)); SharedPtr<int, Free<int>> sp1(p1); FILE* p2 = fopen("test.txt", "r"); SharedPtr<FILE, Fclose<FILE>> sp2(p2); SharedPtr<int> sp3(new int(1));} |

总结:
auto_ptr 管理权的转移 ->不建议使用
scopd_ptr ( unique_ptr ) 防拷贝 ->简单粗暴
shared_ptr 引用计数 ->增减引用计数,直到对象的引用计数为0时再释放
scoped_array 和 shared_array 管理对象数组, 由于scopd_ptr 和 scoped_array、shared_ptr 和 shared_array 的实现都比较类似,这里不再赘述。需要注意的是这两种机制都重载了operator[] .
week_ptr 弱指针 ->辅助shared_ptr解决循环引用的问题。
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-08/134315.htm
