- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
看看 std::weak_ptr
我在几个地方看到它可以用来打破由于使用 std::shared_ptr
的循环依赖导致的内存泄漏>。例如,请参见这两个已接受的答案:[1] , [2] .
采用最后引用的答案,建议的修复是:
#include <memory>
#include <iostream>
struct B;
struct A {
std::shared_ptr<B> b;
~A() { std::cout << "~A()\n"; }
};
struct B {
std::weak_ptr<A> a;
~B() { std::cout << "~B()\n"; }
};
void useAnB() {
auto a = std::make_shared<A>();
auto b = std::make_shared<B>();
a->b = b;
b->a = a;
}
int main() {
useAnB();
std::cout << "Finished using A and B\n";
}
不过,这感觉有点矫枉过正,为什么不简单地使用引用呢?我知道在这个例子中 b->a
没有在构造函数中设置,所以引用不会真正削减它,所以我的问题是:
Is there a reason to use a
std::weak_ptr
instead of a reference if the point is to break circular dependency if we can set it in the constructor?
注意:我理解 std::weak_ptr
用于保存可以失效的引用的用处。我的问题只涉及当要点专门打破循环依赖时的有用性。
最佳答案
这是一个稍微修改过的函数:
void useAnB() {
std::shared_ptr<B> oops;
{
auto a = std::make_shared<A>();
auto b = std::make_shared<B>();
a->b = b;
b->a = a;
oops = b;
}
// use oops->a
}
如果 oops->a
是普通指针或引用,您怎么知道它不再引用有效对象?
关于c++ - 普通引用而不是 weak_ptr 来打破循环依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51152548/
我有以下数据结构: shared_ptr>> foo; 还有一个带有函数的渲染器类: void addObject(weak_ptr _obj) const 这个函数只是将_obj推回 mutable
我了解可用的方法以及它们是什么。请描述 weak_ptr 类的私有(private)部分或给出一些自定义 weak_ptr 代码的示例。我无法通过 std::weak_ptr 实现来理解。 最佳答案
我目前有类似以下内容: class Parent { //just a single child... for sake of simplicity //no other class
以下是 weak_ptr 的 2 个构造函数: http://msdn.microsoft.com/en-us/library/bb982126.aspx weak_ptr(const weak_pt
我有一个类将 weak_ptr 存储在一个容器中,如果 weak_ptr 没有过期,稍后会做一些事情: class Example { public: void fill(std::share
代码如下: struct lex_compare { bool operator() (const weak_ptr &lhs, const weak_ptr &rhs)const {
我想将原始指针成员包装到一些智能指针,以防止在开发类中删除。指针下对象的所有者在类之外。所以,看起来像 boost::shared_ptr 和 std::auto_ptr 不适合。以下是简化的示例:
考虑以下代码: #include #include using namespace std; struct MySharedStruct { int i; }; void print_valu
我了解如何使用 weak_ptr 和 shared_ptr。通过计算对象中的引用数,我了解 shared_ptr 的工作原理。 weak_ptr 是如何工作的?我尝试通读 boost 源代码,但我对
我目前正在为游戏设计一个对象结构,在我的例子中,最自然的组织变成了一棵树。作为智能指针的忠实粉丝,我只使用 shared_ptr 的。然而,在这种情况下,树中的子节点需要访问它的父节点(例如—— ma
根据 [util.smartptr.weak.obs]/4 ,weak_ptr::owner_before 严格弱排序是这样的:“两个 shared_ptr 或 weak_ptr 实例是等价的,当且仅
我正在做一个练习来了解共享指针和弱指针。 所以这个例子是shared_ptr的循环依赖,我该如何解决weak_ptr的问题。 我想将 root->left->parent 初始化为 root,对于右节
看看 std::weak_ptr 我在几个地方看到它可以用来打破由于使用 std::shared_ptr 的循环依赖导致的内存泄漏>。例如,请参见这两个已接受的答案:[1] , [2] . 采用最后引
在我的程序中,Groups 将共享指向 Subjects 的指针;并且 Subjects 将有指向其 Groups 的弱指针。我希望 Group 有一个 join() 函数,将 Subject 的弱指
假设我有一个包含子对象的 shared_ptr 的对象。 我希望子对象有一个指向父对象的 weak_ptr,子对象的构造函数应该是什么样子,我应该如何从父对象构造一个子对象? 提前致谢 最佳答案 由于
序言 在 C++11 中有 std::shared_ptr + std::weak_ptr 组合。尽管非常有用,但它有一个讨厌的问题:你 cannot easily construct shared_
我认为我的问题类似于 shared_ptr and weak_ptr differences ,但我感兴趣的是了解它们如何协同工作,而不是查看差异列表。 维基百科页面 shared_ptr and w
取消引用 weak_ptr 不会自动检查指针是否已过期。为什么要这样设计? 我想安全地取消对弱指针的引用,而不必首先使用 expired() 检查它们,如果它们为空则抛出异常。 这是一个好的做法吗?什
我正在尝试重写我们的 Observer/Observable 实现以使用 std::shared_ptr/std::weak_ptr 来摆脱代码中当前存在的一些讨厌的竞争条件。 通常,观察者会在满足某
我有一个带有自定义删除器的 boost::shared_ptr。将其转换为 weak_ptr 时删除者信息丢失了吗?如果是,我如何将相同的删除器重新附加到 shared_ptr - 从 weak_pt
我是一名优秀的程序员,十分优秀!