一次测试测试(C++11)智能指针引用的崩溃结论
项目中用到了智能指针,但是要在智能指针指向的类中获取此类的shared_ptr指针传给其引用的类,结果出现了问题,
测试代码如下: (包括错误解释)
//测试 shared_ptr weak_ptr map<string,shared_ptr>
#include <stdio.h> //pinrtf ...
#include <string> //string ...
#include <map>
#include <memory> //shared_ptr ... class C; class A {
public:
A() { printf("A()\n"); }
~A() { printf("~A()\n"); } std::string a;
std::weak_ptr<C> with;
virtual std::string geta() = ;
}; class A1:public A {
public:
A1(std::string aa) { a = aa; printf("A1(%s)\n",aa.c_str()); };
~A1() { printf("~A1(%s)\n", a.c_str()); } std::string geta() { std::string b = "A1 " + a; return b; }
}; class A2:public A {
public:
A2(std::string aa) { a = aa; printf("A2(%s)\n", aa.c_str()); };
~A2() { printf("~A2()\n"); printf("~A2(%s)\n", a.c_str()); } std::string geta() { std::string b = "A2 " + a; return b; }
}; typedef std::shared_ptr<A> shrA;
typedef std::shared_ptr<A1> shrA1;
typedef std::shared_ptr<A2> shrA2; template<class _ty>
class AA {
public:
AA() { printf("AA()\n"); }
~AA() { printf("~AA()\n"); } typedef std::map<std::string, _ty> Atype;
Atype list;
}; class C
{
public:
C() { printf("C()\n"); }
~C() { printf("~C()\n"); }//在这个析构函数中下断点会发现进入两次的,两次析构
shrA h1, h2;
std::weak_ptr<A> a; void add(shrA h) {
if (h1 == nullptr) {
h1 = h;
//std::shared_ptr<C> p = (std::shared_ptr<C>)this;//此法虽然不会释放内存了,但是引用计数不对(此处是weak_ptr,但是shared_ptr计数也不对的)
h->with = std::shared_ptr<C>(this); //这样写会在赋值的过程中产生一次创建临时对象(包括构造和析构,会释放掉this的内存)
//因为以这种方式写的话,在这里是用this直接生成了shared_ptr,所以引用计数会变成1,导致在此次析构时释放掉内存
//而导致外面的引用指针指向了被释放掉的内存,,,so,崩溃~~
}
else if(h2 == nullptr) {
h2 = h;
h->with = std::shared_ptr<C>(this);//同上
}
printf("C::add %s\n",h->geta().c_str());
}
};
typedef std::shared_ptr<C> shrC; class CC
{
public :
CC() { printf("CC()\n"); }
~CC() { printf("~CC()\n"); } typedef std::map<std::string, std::shared_ptr<C>> Ctype;
Ctype list;
}; int main() {
{
AA<shrA1> aa1;
AA<shrA2> aa2; CC cc; shrA1 a1 = shrA1(new A1("啊"));
auto b = aa1.list.emplace(std::make_pair("a1", a1)); shrA1 a11 = shrA1(new A1("啊啊"));
b = aa1.list.emplace(std::make_pair("a1", a11));//key 重复,放置失败 (b.seond==false) shrA2 a2(new A2("份饭"));
auto b2 = aa2.list.emplace(std::make_pair("a2", a2)); printf("\n----- --------\n");
for (auto p : aa1.list)
printf("%s\n", p.second->geta().data());
printf("\n----- --------\n");
shrC c(new C());
cc.list.emplace("c1", c);
//c->add(a11); c->add a1/a11/a2 都会导致崩溃
c->add(a2);
}
return ;
}
结论:
在赋值过程中,用形如
std::shared_ptr<C>(this)
的方式取得本指针的shared_ptr指针,会产生新的shared_ptr指针,,,而不是你想要的那个指针(类外定义的那个指针),
虽然其地址貌似是同样的(调试过程中看了下), 但是其实并不是同一个只能指针,
从其计数不一样就可以看出来,
同理, 其他类似的也是一样会构造出新的对象
但是即使是这样写
std::shared_ptr<C> p = (std::shared_ptr<C>)this;
强制类型转换过来的, 也是一个新的智能指针指针对象.
实际上
std::shared_ptr<C>(this)
本身就是一个构造新对象的语句
一次测试测试(C++11)智能指针引用的崩溃结论的更多相关文章
- C++11智能指针之std::unique_ptr
C++11智能指针之std::unique_ptr uniqut_ptr是一种对资源具有排他性拥有权的智能指针,即一个对象资源只能同时被一个unique_ptr指向. 一.初始化方式 通过new云 ...
- c++11 智能指针 unique_ptr、shared_ptr与weak_ptr
c++11 智能指针 unique_ptr.shared_ptr与weak_ptr C++11中有unique_ptr.shared_ptr与weak_ptr等智能指针(smart pointer), ...
- C++11——智能指针
1. 介绍 一般一个程序在内存中可以大体划分为三部分——静态内存(局部的static对象.类static数据成员以及所有定义在函数或者类之外的变量).栈内存(保存和定义在函数或者类内部的变量)和动态内 ...
- 【C++11新特性】 C++11智能指针之weak_ptr
如题,我们今天要讲的是C++11引入的三种智能指针中的最后一个:weak_ptr.在学习weak_ptr之前最好对shared_ptr有所了解.如果你还不知道shared_ptr是何物,可以看看我的另 ...
- 详解C++11智能指针
前言 C++里面的四个智能指针: auto_ptr, unique_ptr,shared_ptr, weak_ptr 其中后三个是C++11支持,并且第一个已经被C++11弃用. C++11智能指针介 ...
- C++11 智能指针
C++ 11标准库引入了几种智能指针 unique_ptr shared_ptr weak_ptr C++内存管理机制是当一个变量或对象从作用域过期的时候就会从内存中将他干掉.但是如果变量只是一个指针 ...
- C++11智能指针
今晚跟同学谈了一下智能指针,突然想要看一下C++11的智能指针的实现,因此下了这篇博文. 以下代码出自于VS2012 <memory> template<class _Ty> ...
- 【C++11新特性】 C++11智能指针之shared_ptr
C++中的智能指针首先出现在“准”标准库boost中.随着使用的人越来越多,为了让开发人员更方便.更安全的使用动态内存,C++11也引入了智能指针来管理动态对象.在新标准中,主要提供了shared_p ...
- C++11智能指针的深度理解
平时习惯使用cocos2d-x的Ref内存模式,回过头来在控制台项目中觉得c++的智能指针有点生疏,于是便重温一下.首先有请c++智能指针们登场: std::auto_ptr.std::unique_ ...
随机推荐
- [Angular Tutorial] 13 -REST and Custom Services
在这一步中,我们将会改变我们获取数据的方式. ·我们定义一个代表RESTful客户端的自定义服务.使用这个客户端,我们可以用一种更简单的方法向服务端请求数据,而不用处理更底层的$httpAPI,HTT ...
- asp.net实现断点续传
C# 断点续传原理与实现 在了解HTTP断点续传的原理之前,让我们先来了解一下HTTP协议,HTTP协议是 一种基于tcp的简单协议,分为请求和回复两种.请求协议是由 客户机(浏览器)向服务器(WEB ...
- eclipse使用egit提交代码报错
在eclipse上使用插件egit同步代码时出现如下错误 解决办法: 开eclipse中的windows-->Preferences-->Team-->Git-->Config ...
- GOLang(第二篇 发起一个Http请求)
import ( "net/http" "net/url" ) //发送一个简单的get请求 func GetRequest { //联系使用 make(map ...
- 《R包的分类介绍》
R分析空间数据(Spatial Data) R机器学习包(Machine Learning) R多元统计包(Multivariate Statistics) R药物(代谢)动力学数据分析包 R计算计量 ...
- Delphi TRect函数例子
{ 在网上看到个这个例子感觉还不错,将它移到自己的博客里没事的时候看看: TRect 作用:保存一个矩形的左上角和右下角的坐标值: 声明: type TRect = ...
- Java Swing paint repaint update 方法的关系
Java Swing paint repaint update 方法的关系: 参考:http://blog.csdn.net/xiaoliangmeiny/article/details/691665 ...
- 移动设备应用程序中支持多个屏幕大小和 DPI 值
支持多个屏幕大小和 DPI 值的指导原则 要部署独立于平台的应用程序,应了解不同的输出设备.设备可以具有不同的屏幕大小或分辨率以及不同的 DPI 值或密度. Flex 工程师 Jason SJ 在他的 ...
- Nginx之RTMP
1. RTMP协议介绍 RTMP(Real Time Messaging Protocol)实时消息传送协议是Adobe Systems公司为Flash播放器和服务器之间音频.视频和数据传输开发的私有 ...
- 在windows下安装运行disconf
一.环境准备 1.下载disconf源码 (本文以2.6.36为例) https://github.com/knightliao/disconf 2.下载nginx1.10.2(windows版) h ...