c++ shared_ptr的错误用法之cycle引用
来源:互联网 发布:ubuntu下如何挂载u盘 编辑:程序博客网 时间:2024/06/05 00:17
#include <iostream>#include <memory>#include <string>#include <vector>using namespace std;class Person{public: string name; shared_ptr<Person> monther; shared_ptr<Person> father; vector<shared_ptr<Person>> kids;public: Person(const string& n,shared_ptr<Person> m = nullptr,shared_ptr<Person> f = nullptr):name(n),monther(m),father(f) {} ~Person() { cout << "delete " << name << endl; system("pause"); }};shared_ptr<Person> initFamily(const string& name){ shared_ptr<Person> mom(new Person(name + "'s monther")); shared_ptr<Person> dad(new Person(name + "'s father")); shared_ptr<Person> kid(new Person(name, mom, dad)); mom->kids.push_back(kid);//++use_count; dad->kids.push_back(kid);//++use_count; return kid; //++use_count;}//--use_count;int main(){ shared_ptr<Person> kid = initFamily("Marco");//++use_count; cout << kid.use_count() << endl; //因shared_ptr形成cycle无法释放,故无法调用析构函数 //下面的system("pause")则必须有,否则会一闪而过 system("pause"); return 0;}
//解决办法:使用weak_ptr
#include <iostream>#include <memory>#include <string>#include <vector>using namespace std;class Person{public: string name; shared_ptr<Person> monther; shared_ptr<Person> father; vector<weak_ptr<Person>> kids;public: Person(const string& n, shared_ptr<Person> m = nullptr, shared_ptr<Person> f = nullptr):name(n),monther(m),father(f) { } ~Person() { cout << "detele " << name << endl; }};shared_ptr<Person> initFamily(const string& name){ shared_ptr<Person> mom(new Person(name + "'s monther")); shared_ptr<Person> dad(new Person(name + "'s father")); shared_ptr<Person> kid(new Person(name, mom, dad)); weak_ptr<Person> w_kid(kid); mom->kids.push_back(w_kid); dad->kids.push_back(w_kid); return kid;}int main(){ shared_ptr<Person> p = initFamily("Marco"); cout << p.use_count() << endl; cout << "the kid is " << p->name << endl; cout << "the kid's monther " << p->monther->name << endl; cout << "the kid's father " << p->father->name << endl; for (auto &v : p->monther->kids) if (!v.expired()) cout << v.lock()->name << endl; //析构函数会调用,则下面system("pause")不需要添加 //另外通过命令行编译更直观 //system("pause"); return 0;}
0 0
- c++ shared_ptr的错误用法之cycle引用
- shared_ptr 之循环引用 weak_ptr
- shared_ptr的用法
- shared_ptr的用法
- std::shared_ptr 和 std::weak_ptr的用法以及引用计数的循环引用问题
- std::shared_ptr 和 std::weak_ptr的用法以及引用计数的循环引用问题
- std::shared_ptr 和 std::weak_ptr的用法以及引用计数的循环引用问题
- std::shared_ptr 和 std::weak_ptr的用法以及引用计数的循环引用问题
- boost的shared_ptr循环引用
- boost的shared_ptr循环引用
- boost的shared_ptr循环引用
- shared_ptr的引用计数原理
- C++:智能指针之shared_ptr
- 浅谈shared_ptr及shared_ptr涉及到的循环引用问题
- 智能指针shared_ptr的用法
- shared_ptr与make_shared的用法
- 智能指针shared_ptr的用法
- 引用 Invoke的用法(C#)
- 线程安全与可重入函数的区别
- 李飞飞发表研究新成果:视觉推理的推断和执行程序(HR)
- C/C++:大写字母转化为小写字母
- Python基础练习2
- python3 小技巧记录(此文档会不定期更新)
- c++ shared_ptr的错误用法之cycle引用
- Java安全(JCA/JSSE):Java加密框架(JCA)简要说明
- 【怎样写代码】参数化类型 -- 泛型(五):泛型类
- ORB-SLAM2安装与运行(非ROS环境下)
- Eclipse加载插件失败
- Leetcode:Word Ladder
- 运维_win server2008关闭危险端口445,135,137,138,139的方法
- Tensorflow实现卷积神经网络模型
- 解决jad.exe查看外部jar包源码失败问题