项目中用到了智能指针,但是要在智能指针指向的类中获取此类的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高级程序设计-13:事件

    JavaScript与HTML之间的交互是通过事件实现的. 一.事件流 首先我们要明白事件流的概念.当我们点击一个按钮时,也点击了按钮的容器元素,甚至也点击了整个事件.事件流描述就是从页面中接收事件的 ...

  2. iOS 之 UITextView

    _lableAssess = [[UITextView alloc] init]; [_lableAssess setFrame:CGRectMake(left2, top2, width2, siz ...

  3. 让表格table呈现边框,不用给tr、td加边框的写法

    <table width="100%" cellspacing="1" cellpadding="1" bgcolor="# ...

  4. Heka 编译安装后 运行报错 panic: runtime error: cgo argument has Go pointer to Go pointer

    Heka 编译安装后 运行报错 panic: runtime error: cgo argument has Go pointer to Go pointer 解决办法: 1.  Start heka ...

  5. .NET DLL 保护措施详解(非混淆加密加壳)核心思路的实现

    最近有很多朋友通过BLOG找到我询问我的相关细节,其实相关的实现细节我早已把源码上传到51aspx上面了,地址是http://www.51aspx.com/code/codename/56949 也有 ...

  6. Intent的属性及Intent-filter配置——实例Action、Data属性启动系统Activity

    一旦为Intent同时指定了Action.Data属性,那么Android将可根据指定的数据类型来启动特定的应用程序,并对指定数据类型执行相应的操作. 下面是几个Action属性.Data属性的组合. ...

  7. Java线程: 线程调度

    线程调度是Java多线程的核心,只有好的调度,才能充分发挥系统的性能,提高程序的执行效率. 一.休眠 休眠的目的是使线程让出CPU的最简单做法,线程休眠的时候,会将CPU交给其他线程,以便轮换执行,休 ...

  8. Bootstrap 输入组

    Bootstrap 输入组: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  9. js运算符单竖杠“|”的作用

    在js整数操作的时候,相当于去除小数点,parseInt.在正数的时候相当于Math.floor(),负数的时候相当于Math.ceil() 注: 1. Math.ceil()用作向上取整. 2. M ...

  10. Android 自定义通用的loadingview

    介绍 好久没有写博客啦,最近在接近新年了,年前的工作都要收尾,所以特别忙,周末抽空写了个通用的加载view,写篇博客分享出来. 功能 1.显示加载视图,加载失败的时候显示加载失败视图,数据为空时显示数 ...