营销方案和销售思路,免费网站seo软件,广州网站建设公司乐云seo,做美团一样的网站需要多少钱shared_ptr使用的注意事项: 1.不能使用一个原始地址初始化多个共享智能指针 2.函数不能返回管理了this的共享智能指针对象 3.共享智能指针不能循环引用 不能使用一个原始地址初始化多个共享智能指针
代码如下:
#include iostream
#include memory
using name…shared_ptr使用的注意事项: 1.不能使用一个原始地址初始化多个共享智能指针 2.函数不能返回管理了this的共享智能指针对象 3.共享智能指针不能循环引用 不能使用一个原始地址初始化多个共享智能指针
代码如下:
#include iostream
#include memory
using namespace std;//shared_ptr使用的注意事项:
//1.不能使用一个原始地址初始化多个共享智能指针
//2.函数不能返回管理了this的共享智能指针对象
//3.共享智能指针不能循环引用struct Test
{shared_ptrTest getSharedPtr(){return shared_ptrTest(this);}~Test(){cout class Test is disstruct ... endl;}
};int main()
{Test *t new Test;shared_ptrTest ptr1(t);shared_ptrTest ptr2(t);return 0;
}
测试结果: 这块堆内存被析构了两次所以出现了错误。
正确写法:
int main()
{Test *t new Test;shared_ptrTest ptr1(t);shared_ptrTest ptr2 ptr1;return 0;
}
测试结果: 函数不能返回管理了this的共享智能指针对象
代码如下:
#include iostream
#include memory
using namespace std;//shared_ptr使用的注意事项:
//1.不能使用一个原始地址初始化多个共享智能指针
//2.函数不能返回管理了this的共享智能指针对象
//3.共享智能指针不能循环引用struct Test
{shared_ptrTest getSharedPtr(){return shared_ptrTest(this);}~Test(){cout class Test is disstruct ... endl;}
};int main()
{shared_ptrTest sp1(new Test);cout use_count sp1.use_count() endl;shared_ptrTest sp2 sp1-getSharedPtr();cout use_count sp1.use_count() endl;return 0;
}
测试结果: 通过输出的结果可以看到一个对象被析构了两次其原因是这样的在这个例子中使用同一个指针 this 构造了两个智能指针对象 sp1 和 sp2这二者之间是没有任何关系的因为 sp2 并不是通过 sp1 初始化得到的实例对象。在离开作用域之后 this 将被构造的两个智能指针各自析构导致重复析构的错误。
这个问题可以通过 weak_ptr 来解决通过 wek_ptr 返回管理 this 资源的共享智能指针对象 shared_ptr。C11 中为我们提供了一个模板类叫做 std::enable_shared_from_this这个类中有一个方法叫做 shared_from_this()通过这个方法可以返回一个共享智能指针在函数的内部就是使用 weak_ptr 来监测 this 对象并通过调用 weak_ptr 的 lock() 方法返回一个 shared_ptr 对象。
使用weak_ptr解决shared_ptr管理的内存被重复析构的问题
代码如下:
#include iostream
#include memory
using namespace std;//shared_ptr使用的注意事项:
//1.不能使用一个原始地址初始化多个共享智能指针
//2.函数不能返回管理了this的共享智能指针对象
//3.共享智能指针不能循环引用struct Test:public enable_shared_from_thisTest
{shared_ptrTest getSharedPtr(){return shared_from_this();}~Test(){cout class Test is disstruct ... endl;}
};int main()
{shared_ptrTest sp1(new Test);cout use_count sp1.use_count() endl;shared_ptrTest sp2 sp1-getSharedPtr();cout use_count sp1.use_count() endl;return 0;
}
测试结果: 共享智能指针不能循环引用
代码如下:
#include iostream
#include memory
using namespace std;struct TA;
struct TB;struct TA
{shared_ptrTB bptr;~TA(){cout class TA is disstruct... endl;}
};struct TB
{shared_ptrTA aptr;~TB(){cout class TB is disstruct endl;}
};void test()
{shared_ptrTA ap(new TA);shared_ptrTB bp(new TB);cout TA use_count ap.use_count() endl;cout TB use_count bp.use_count() endl;ap-bptr bp;bp-aptr ap;cout TA use_count ap.use_count() endl;cout TB use_count bp.use_count() endl;}int main()
{test();return 0;
}测试结果: 在测试程序中共享智能指针 ap、bp 对 TA、TB 实例对象的引用计数变为 2在共享智能指针离开作用域之后引用计数只能减为1这种情况下不会去删除智能指针管理的内存导致类 TA、TB 的实例对象不能被析构最终造成内存泄露。通过使用 weak_ptr 可以解决这个问题只要将类 TA 或者 TB 的任意一个成员改为 weak_ptr修改之后的代码如下
#include iostream
#include memory
using namespace std;struct TA;
struct TB;struct TA
{weak_ptrTB bptr;~TA(){cout class TA is disstruct... endl;}
};struct TB
{shared_ptrTA aptr;~TB(){cout class TB is disstruct endl;}
};void test()
{shared_ptrTA ap(new TA);shared_ptrTB bp(new TB);cout TA use_count ap.use_count() endl;cout TB use_count bp.use_count() endl;ap-bptr bp;bp-aptr ap;cout TA use_count ap.use_count() endl;cout TB use_count bp.use_count() endl;}int main()
{test();return 0;
}测试结果: 通过输出的结果可以看到类 TA 或者 TB 的对象被成功析构了。
上面程序中在对类 TA 成员赋值时 ap-bptr bp; 由于 bptr 是 weak_ptr 类型这个赋值操作并不会增加引用计数所以 bp 的引用计数仍然为 1在离开作用域之后 bp 的引用计数减为 0类 TB 的实例对象被析构。
在类 TB 的实例对象被析构的时候内部的 aptr 也被析构其对 TA 对象的管理解除内存的引用计数减为 1当共享智能指针 ap 离开作用域之后对 TA 对象的管理也解除了内存的引用计数减为 0类 TA 的实例对象被析构。