-
带引用计数的智能指针
shared_ptr
的一个常见问题#include<iostream> #include<memory> using namespace std; int main() { int* p = new int(); auto ptr_1 = shared_ptr<int>(p); auto ptr_2 = shared_ptr<int>(p); cout << ptr_1.use_count() << " " //1 << ptr_2.use_count() << endl; //1 system("pause"); return 0; }
上面通过普通指针
p
来初始化两个shared_ptr指针
,会导致p指向的堆内存重复释放
,因为这个两个shared_ptr对p指向的内存的引用计数都是1
。 -
正确做法:
#include<iostream> #include<memory> using namespace std; int main() { int* p = new int(); auto ptr_1 = shared_ptr<int>(p); auto ptr_2 = ptr_1 ; cout << ptr_1.use_count() << " " //2 << ptr_2.use_count() << endl; //2 system("pause"); return 0; }
应该通过
拷贝构造
,来保证不同的shared_ptr对相同的堆内存引用计数保持一致,当每个share_ptr释放的时候引用计数-1,引用计数到0的时候释放堆内存。 -
enable_shared_from_this:
-
是个可以被继承的模板类,里面有个
shared_from_this()
的成员函数,可以返回指向当前对象的智能指针shared_ptr,并保持对当前对象引用计数的一致性
; -
先看下面一段存在问题代码:
#include<iostream> #include<memory> using namespace std; class A :public enable_shared_from_this<A> { public: shared_ptr<A> get() { return shared_ptr<A>(this); 如果通过this指针传递当前指向当前对象的智能指针会导致引用计数一致 } }; int main() { A* p = new A(); auto ptr_1 = shared_ptr<A>(p); auto ptr_2(ptr_1->get()); cout << ptr_1.use_count() << " " //1 << ptr_2.use_count() << endl; //1 system("pause"); return 0; }
如果当前实例对象被智能指针管理, 想通过实例对象的成员函数传递指向当前对象的shared_ptr,直接通过this指针构建一个shared_ptr是不安全的,会导致当前对象被重复析构。
-
如果当前对象被shared_ptr管理,且需要在当前对象内部的成员函数中返回当前指向当前对象的
shared_ptr
,并保持引用计数一致,就可以使用enable_shared_from_this
。#include<iostream> #include<memory> using namespace std; class A :public enable_shared_from_this<A> { public: shared_ptr<A> get() { return shared_from_this(); } }; int main() { A* p = new A(); auto ptr_1 = shared_ptr<A>(p); auto ptr_2(ptr_1->get()); cout << ptr_1.use_count() << " " //2 << ptr_2.use_count() << endl; //2 system("pause"); return 0; }
-
智能指针:关于enable_shared_from_this的使用
于 2023-07-13 15:35:08 首次发布