项目中用到了智能指针,但是要在智能指针指向的类中获取此类的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)智能指针引用的崩溃结论的更多相关文章

  1. C++11智能指针之std::unique_ptr

    C++11智能指针之std::unique_ptr   uniqut_ptr是一种对资源具有排他性拥有权的智能指针,即一个对象资源只能同时被一个unique_ptr指向. 一.初始化方式 通过new云 ...

  2. 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), ...

  3. C++11——智能指针

    1. 介绍 一般一个程序在内存中可以大体划分为三部分——静态内存(局部的static对象.类static数据成员以及所有定义在函数或者类之外的变量).栈内存(保存和定义在函数或者类内部的变量)和动态内 ...

  4. 【C++11新特性】 C++11智能指针之weak_ptr

    如题,我们今天要讲的是C++11引入的三种智能指针中的最后一个:weak_ptr.在学习weak_ptr之前最好对shared_ptr有所了解.如果你还不知道shared_ptr是何物,可以看看我的另 ...

  5. 详解C++11智能指针

    前言 C++里面的四个智能指针: auto_ptr, unique_ptr,shared_ptr, weak_ptr 其中后三个是C++11支持,并且第一个已经被C++11弃用. C++11智能指针介 ...

  6. C++11 智能指针

    C++ 11标准库引入了几种智能指针 unique_ptr shared_ptr weak_ptr C++内存管理机制是当一个变量或对象从作用域过期的时候就会从内存中将他干掉.但是如果变量只是一个指针 ...

  7. C++11智能指针

    今晚跟同学谈了一下智能指针,突然想要看一下C++11的智能指针的实现,因此下了这篇博文. 以下代码出自于VS2012 <memory> template<class _Ty> ...

  8. 【C++11新特性】 C++11智能指针之shared_ptr

    C++中的智能指针首先出现在“准”标准库boost中.随着使用的人越来越多,为了让开发人员更方便.更安全的使用动态内存,C++11也引入了智能指针来管理动态对象.在新标准中,主要提供了shared_p ...

  9. C++11智能指针的深度理解

    平时习惯使用cocos2d-x的Ref内存模式,回过头来在控制台项目中觉得c++的智能指针有点生疏,于是便重温一下.首先有请c++智能指针们登场: std::auto_ptr.std::unique_ ...

随机推荐

  1. javascript--正则表达式--更新中

    引用地址:http://www.iteye.com/topic/481228 和http://www.cnblogs.com/rubylouvre/archive/2010/03/09/1681222 ...

  2. Spring Security——核心类简介——获得登录用户的相关信息

    核心类简介 目录 1.1     Authentication 1.2     SecurityContextHolder 1.3     AuthenticationManager和Authenti ...

  3. UILable添加事件

    原文:http://blog.sina.com.cn/s/blog_9e8867eb0101dk6t.html 先需要声明的是:UILabel或UIImageView的 userInteraction ...

  4. checkbox、radio控件和文字不对其

    一般使用html控件的时候  单选按钮和复选框的控件和文字不对齐 给input控件加上   style="vertical-align: middle; margin-top: -2px; ...

  5. CentOS下架设Telnet服务器

    CentOS下架设Telnet服务器1.什么是Telnet?来自度娘的解释:Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机 ...

  6. 封装 INI 文件读写函数

    delphi读写ini文件实例 //--两个过程,主要实现:窗体关闭的时候,文件保存界面信息:窗体创建的时候,程序读取文件文件保存的信息. //--首先要uses IniFiles(单元) //--窗 ...

  7. Notification的功能与用法

    Notification是显示在手机状态的通知——手机状态栏位于手机屏幕的最上方,那里一般显示了手机当前的网络状态.时间等.Notification所代表的是一种具有全局效果的通知,程序一般通过Not ...

  8. php判断IE浏览器

    <?php/** * 检测用户当前浏览器 * @return boolean 是否ie浏览器 */ function chk_ie_browser() { $userbrowser = $_SE ...

  9. cssText 和 this

    一.cssText 元素.style.width = '200px';   ==>   元素.style.cssText = 'width:200px;height:200px;' 二.this ...

  10. 安装使用GYP,并编译libpomelo2

    1.然后是下载GYP,它是由 Chromium 团队开发的跨平台自动化项目构建工具,安装后可以使用其构建出libpomelo的vs工程项目,再而进行编译,这步先下载,地址: http://code.g ...