C++ 智能指针五
/*
代码分析: 这是标准库的源码,我们看到在enable_shared_from_this内部保存了一个weak_ptr。shared_from_this函数就是通过这个weak_ptr得到了。
但是另外一点,我们可以看到在enable_shared_from_this的构造函数中并没有对这个weak_ptr进行初始化。
这就是为什么我们不能在构造函数调用shared_from_this()的原因,因为其内部的weak_ptr并没有初始化。所以会产生错误。 在实际的编程中如果我们需要在对象初始化中用到自己的shared_ptr。可
以单独将初始化操作放到一个独立的init函数中,这时候再调用shared_from_this()是没有问题的(但还是有点问题,下面会讲到) 熟悉weak_ptr的同学可能知道,我们在使用weak_ptr前,需要用一个shared_ptr来对其进行初始化。
对weak_ptr初始化是要能获取到当前对象的引用计数对象,而引用计数对象可以通过shared_ptr对象获取到。
当然我们同样可以用一个已经初始化过的weak_ptr来初始化另一个weak_ptr,因为已初始化的weak_ptr也可能获取到对象的引用计数。 enable_shared_from_this内部的weak_ptr是通过_Do_enable函数初始化的。
而_Do_enable函数实在shared_ptr的构造函数中调用的,这是至关重要的一个环节。
正因为如此我们在调用shared_from_this之前请确保程序已经显式地创建了shared_ptr对象,
要不然enable_shared_from_this内部的weak_ptr始终是无效。 同理在析构函数中也不能调用shared_from_this()。
在析构时,引用计数已经变为零,weak_ptr已经相当于指向的是一个无效的对象,这是不能通过此无效的weak_ptr构造shared_ptr。 */ template<class _Ty>
class enable_shared_from_this
{
// provide member functions that create shared_ptr to this
public:
typedef _Ty _EStype; shared_ptr<_Ty> shared_from_this()
{ // return shared_ptr
return (shared_ptr<_Ty>(_Wptr));
} shared_ptr<const _Ty> shared_from_this() const
{ // return shared_ptr
return (shared_ptr<const _Ty>(_Wptr));
} protected:
enable_shared_from_this()
{ // construct (do nothing)
} enable_shared_from_this(const enable_shared_from_this&)
{ // construct (do nothing)
} enable_shared_from_this& operator=(const enable_shared_from_this&)
{ // assign (do nothing)
return (*this);
} ~enable_shared_from_this()
{ // destroy (do nothing)
} private:
//友元函数
template<class _Ty1,class _Ty2>
friend void _Do_enable(_Ty1 *, enable_shared_from_this<_Ty2>*, _Ref_count_base *); mutable weak_ptr<_Ty> _Wptr;
}; template<class _Ty1,class _Ty2>
inline void _Do_enable(_Ty1 *_Ptr, enable_shared_from_this<_Ty2> *_Es, _Ref_count_base *_Refptr)
{ // reset internal weak pointer
_Es->_Wptr._Resetw(_Ptr, _Refptr);
}
/* 智能指针shared_from_this崩溃问题分析 */ #include <iostream>
#include <memory> class TestClass : public std::enable_shared_from_this<TestClass>
{
public:
TestClass()
{
}
~TestClass()
{
} void show()
{
printf("hello world .\n");
} std::shared_ptr<TestClass> getPtr()
{
return shared_from_this();
}
}; int main()
{
TestClass t;
t.getPtr(); //shared_from_this()错误 TestClass* t1(new TestClass());
t1->getPtr();//shared_from_this()错误 std::shared_ptr<TestClass> t2(new TestClass());
t2->getPtr(); //正确,已提前创建了shared_ptr
}
C++ 智能指针五的更多相关文章
- 必须要注意的 C++ 动态内存资源管理(五)——智能指针陷阱
必须要注意的 C++ 动态内存资源管理(五)——智能指针陷阱 十三.小心使用智能指针. 在前面几节已经很详细了介绍了智能指针适用方式.看起来,似乎智能指针很强大,能够很方便很安全的管理 ...
- 【C++】智能指针简述(五):解决循环引用的weak_ptr
总结一下前文内容: 1.智能指针通过RAII方法来管理指针:构造对象时,完成资源初始化;析构对象时,对资源进行清理及汕尾. 2.auto_ptr,通过“转移所有权”来防止析构一块内存多次.(如何转移? ...
- 基于C/S架构的3D对战网络游戏C++框架_05搭建系统开发环境与Boost智能指针、内存池初步了解
本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...
- c++ auto_ptr智能指针
c++ auto_ptr智能指针 该类型在头文件memory中,在程序的开通通过 #include<memory> 导入,接下来讲解该智能指针的作用和使用. 使用方法: auto_ptr& ...
- Android智能指针sp wp详解
研究Android的时候,经常会遇到sp.wp的东西,网上一搜,原来是android封装了c++中对象回收机制.说明:1. 如果一个类想使用智能指针,那么必须满足下面两个条件: a. 该类是虚基 ...
- STL模板_智能指针概念
一.智能指针1.类类型对象,在其内部封装了一个普通指针.当智能指针对象因离开作用域而被析构时,其析构函数被执行,通过其内部封装的普通指针,销毁该指针的目标对象,避免内存泄露.2.为了表现出和普通指针一 ...
- 论C++的智能指针
一.简介 参考这篇博客,并且根据<C++ Primer>中相关知识,我总结了C++关于智能指针方面的内容. 为了解决内存泄漏的问题,便出现了智能指针.STL提供的智能指针有:aut ...
- DirectX11--ComPtr智能指针
综述 DirectX11 With Windows SDK完整目录 欢迎加入QQ群: 727623616 可以一起探讨DX11,以及有什么问题也可以在这里汇报. IUnknown接口类 DirectX ...
- c/c++ 智能指针 shared_ptr 和 new结合使用
智能指针 shared_ptr 和 new结合使用 用make_shared函数初始化shared_ptr是最推荐的,但有的时候还是需要用new关键字来初始化shared_ptr. 一,先来个表格,唠 ...
随机推荐
- element对象
一.offset.client.scroll clientWidth,clientHeight,clientLeft,clientTop: offsetWidth,offsetHeight,offse ...
- Python学习——深浅拷贝
1.对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. >>> import copy # ######### 数字.字符串 ######### ...
- Luogu2570 [ZJOI2010]贪吃的老鼠 ---- 网络流
Luogu2570 [ZJOI2010]贪吃的老鼠 题面描述 https://www.luogu.org/problemnew/show/P2570 然后题意大概就是m只老鼠,然后吃n个奶酪,已知 ...
- win7查看其它工作组 win7 所有工作组
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha win7 所有工作组 ==== win7 网络 工作组 查找 自身有问题. 多刷新几 ...
- JavaScript_作用域(2017-03-16)
这里写的还不够清楚. 建议去看:王福朋 的博客的加深理解. 深入理解javascript原型和闭包(8)——简述[执行上下文]上 深入理解javascript原型和闭包(9)——简述[执行上下文]下 ...
- java有时候String a="zz"出现String cannot be resolved to a variable
原因很简单在String a="zz"前面有些该注释的没注释导致
- 将java项目传输到centos7服务端
http://www.xdowns.com/so.asp?keyword=flashfxp 下载flashfxp之后进行一系列配置即可 https://cloud.baidu.com/?from=co ...
- epoll惊群原因分析
考虑如下情况(实际一般不会做,这里只是举个例子): 在主线程中创建一个socket.绑定到本地端口并监听 在主线程中创建一个epoll实例(epoll_create(2)) 将监听socket添加到e ...
- ZJUT 地下迷宫 (高斯求期望)
ShowID=1423">http://cpp.zjut.edu.cn/ShowProblem.aspx?ShowID=1423 设dp[i]表示在i点时到达终点要走的期望步数,那么d ...
- 使用uploadify上传图片时返回“Cannot read property 'queueData' of undefined”
在使用uploadify插件上传图片时,遇到一个比较坑的错误:上传时提示“Cannot read property 'queueData' of undefined”. 遇到这个问题有点无语,因为这个 ...