使用weak_ptr打破循环引用是解决C++中shared_ptr导致内存泄漏的关键方法,通过将双向强引用改为单向shared_ptr加weak_ptr,避免引用计数无法归零;同时可通过减少双向依赖、使用原始指针、手动断开连接或引入管理类等方式解耦对象关系,确保资源正确释放。

在C++中,循环引用通常出现在两个或多个类相互持有对方的实例(尤其是指针或引用)时,导致内存无法正确释放,特别是在使用智能指针时容易引发资源泄漏。解决这类问题的关键是打破强引用环。以下是几种常见的解决方法。
解决方案是将其中一个引用改为 weak_ptr,它不会增加引用计数,只在需要时临时升级为 shared_ptr 来访问对象。
示例:
#include <memory>
#include <iostream>
<p>class B; // 前向声明</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/6e7abc4abb9f" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">C++免费学习笔记(深入)</a>”;</p><p>class A {
public:
std::shared_ptr<B> ptr;
~A() { std::cout << "A destroyed\n"; }
};</p><p>class B {
public:
std::weak_ptr<A> ptr; // 使用 weak_ptr 避免循环
~B() { std::cout << "B destroyed\n"; }
};</p><p>int main() {
auto a = std::make_shared<A>();
auto b = std::make_shared<B>();</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">a->ptr = b;
b->ptr = a; // 不会增加引用计数
return 0; // 正常析构 A 和 B} 在这个例子中,A 持有 B 的 shared_ptr,而 B 持有 A 的 weak_ptr,打破了循环引用,确保对象能被正确释放。
例如,在父子关系中,父对象持有子对象的 shared_ptr,子对象只需保存父对象的原始指针(raw pointer),前提是父对象生命周期一定长于子对象。
在线证件照系统是一套完善的冲印行业解决方案,致力于解决用户线上拍摄证件照,拍摄最美最标准证件照的使命。证件照免费版功能:后台统计:当天制作、当天新增、支持规格、近7日统计规格列表:筛选查看、编辑用户列表:筛选查看常见问题:筛选查看、新增、编辑、删除小程序设置:应用设置、流量主设置小程序跳转:筛选查看、新增、编辑、删除关注公众号:引导设置系统要求:系统:Linux系统(centos x64)运行环境
1
示例:
class Parent;
<p>class Child {
public:
Parent* parent; // 只保存原始指针,不参与生命周期管理
void doSomething() { parent->action(); }
};</p><p>class Parent {
public:
std::shared_ptr<Child> child;
Parent() {
child = std::make_shared<Child>();
child->parent = this;
}
void action() { std::cout << "Parent action\n"; }
};
示例:
class Node {
public:
std::shared_ptr<Node> next;
std::shared_ptr<Node> prev;
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">~Node() {
next.reset();
prev.reset(); // 主动断开引用
}}; 虽然这种方法可行,但容易出错,建议优先使用 weak_ptr。
比如定义一个 Manager 类负责维护 A 和 B 的关系,而不是让它们互相持有。
基本上就这些。关键是理解引用关系,合理使用 weak_ptr 和设计对象依赖方向。
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号