前言
shared_ptr实现共享式拥有概念。多个智能指针可以指向相同对象,该对象和其相关资源会在“最后一个引用被销毁”时候释放。从名字share就可以看出了资源可以被多个指针共享,它使用计数机制来表明资源被几个指针共享。可以通过成员函数use_count()来查看资源的所有者个数。除了可以通过new来构造,还可以通过传入auto_ptr, unique_ptr,weak_ptr来构造。当我们调用release()时,当前指针会释放资源所有权,计数减一。当计数等于0时,资源会被释放。
一、成员函数
- use_count,返回引用计数的个数
- unique,返回是否是独占所有权( use_count 为 1)
- swap,交换两个 shared_ptr 对象(即交换所拥有的对象)
- reset,放弃内部对象的所有权或拥有对象的变更, 会引起原有对象的引用计数的减少
- get,返回内部对象(指针), 由于已经重载了()方法, 因此和直接使用对象是一样的.
二、使用例子
#include <iostream>
//引入库
#include <memory>
using namespace std;
int main()
{
string *s1 = new string("s1");
shared_ptr<string> ps1(s1);
shared_ptr<string> ps2;
cout << ps1.use_count()<<endl; //指向了s1,所以引用计数+1,为1
cout<<ps2.use_count()<<endl; //没有指向,引用计数为0
cout << ps1.unique()<<endl; //1
ps2 = ps1;
cout << ps1.use_count()<<endl; //2
cout<<ps2.use_count()<<endl; //2
cout << ps1.unique()<<endl; //非独占,所以为0
cout << *ps1 <<endl; //s1
cout << *ps2 <<endl; //s1
string *s3 = new string("s3");
shared_ptr<string> ps3(s3);
cout << (ps1.get()) << endl; //0x1b91010
cout << ps3.get() << endl; //0x1b91080
swap(ps1, ps3); //交换所拥有的对象,ps1和ps3指向的地址交换了
cout << (ps1.get())<<endl; //0x1b91080
cout << ps3.get() << endl; //0x1b91010
cout << *ps1 <<endl; //s3
cout << *ps3 <<endl; //s1
cout << ps1.use_count()<<endl; //1
cout << ps2.use_count() << endl; //2
cout << ps3.use_count() << endl; //2
ps2 = ps1; //ps2切换了指向,所以ps3(0x1b91010) 的引用计数再减1
cout << ps1.use_count()<<endl; //2
cout << ps2.use_count() << endl; //2
cout << ps3.use_count() << endl; //1
ps1.reset(); //放弃ps1的拥有权,ps2的引用计数的减少
cout << ps1.use_count()<<endl; //0
cout << ps2.use_count()<<endl; //1
cout << ps3.use_count() << endl; //1
return 0;
}
总结
share_ptr虽然已经很好用了,但是有一点share_ptr智能指针还是有内存泄露的情况:当两个对象相互使用一个shared_ptr成员变量指向对方,会造成循环引用,使引用计数失效,引用计数一直不为0、对象无法析构,从而导致内存泄漏。
要解决这个问题,就需要使用 weak_ptr!