[英]Debug assertion failure - C++, using smart pointers
I have been trying to debug this for a while now without any luck.我一直在尝试对此进行调试,但没有任何运气。 I was hoping to get some help here.
我希望能在这里得到一些帮助。 Apologies if my question isn't relevant or something, I'm new.
如果我的问题不相关或其他问题,我很抱歉,我是新手。
So basically what I have is:所以基本上我所拥有的是:
#include <iostream>
template<typename T> class Node {
using NodePtr = std::shared_ptr<Node<T>>;
private:
Node() {}
T value{};
NodePtr parent;
NodePtr child;
public:
Node(const T& value) : value{ value } {}
Node(const T& value, NodePtr child) : Node(value)
{
this->child = child;
if (child != NULL)
{
//problem here?
child->parent = NodePtr(this);
/*The equivalent in C# works perfectly fine:*/
/*this.child = child;
if(child != null) {
child.parent = this;
}*/
}
}
const T& Value() const { return value; }
T& ValueRef() const { return value; }
NodePtr Parent() const { return parent; }
NodePtr Child() const { return child; }
};
template<typename T> std::ostream& operator<<(std::ostream& stream, const Node<T>& node) {
stream << "{" << node.Value() << "}";
return stream;
}
int main()
{
Node<int> n{ 5, std::make_shared<Node<int>>(3) };
std::cout << n;
}
I can implement this easily without using smart pointers, but I'm trying to learn them so there's that.我可以在不使用智能指针的情况下轻松实现这一点,但我正在尝试学习它们,所以就是这样。
The assertion that is failing: "is_block_type_valid(header->_block_use)"失败的断言:“is_block_type_valid(header->_block_use)”
Image of the assertion error断言错误的图像
Any help would be appreciated, thank you.任何帮助将不胜感激,谢谢。
There are a few problems.有几个问题。 First, if you want to get a shared_ptr from this, you have to inherit from std::enable_shared_from_this:
首先,如果你想从中得到一个 shared_ptr,你必须从 std::enable_shared_from_this 继承:
template<typename T> class Node : std::enable_shared_from_this<Node<T>> {
// ...
The main problem is that there is a shared_ptr referencing a shared_ptr (the parent to the child, vice-versa).主要问题是有一个 shared_ptr 引用了一个 shared_ptr(父级到子级,反之亦然)。 However, shared_ptr are only reference-counted.
但是, shared_ptr 只是引用计数。 Take a look at the program below:
看看下面的程序:
#include <iostream>
#include <memory>
struct A;
struct B{
std::shared_ptr<A> sp;
~B(){ std::cout << "B destroyed\n"; }
};
struct A{
std::shared_ptr<B> sp;
~A(){ std::cout << "A destroyed\n"; }
};
int main(){
std::shared_ptr<B> b{};
std::shared_ptr<A> a {b->sp};
b = a->sp;
}
The output is blank!输出为空! Here is a link to the above program.
这是上述程序的链接。
To fix, do two additional things: one, change child->parent = NodePtr(this);
要修复,请做另外两件事:一,更改
child->parent = NodePtr(this);
to the following:到以下几点:
child->parent = this->weak_from_this();
Also, change the type of parent
to std::weak_ptr<Node<T>>
.另外,将
parent
的类型更改为std::weak_ptr<Node<T>>
。
std::weak_ptr
is used with problems like this. std::weak_ptr
用于处理此类问题。 It doesn't influence the reference count of the std::shared_ptr
For more information, go here .它不会影响
std::shared_ptr
的引用计数有关更多信息,请转到此处。
PS the error about the is_block_type was caused by shared_ptr, probably because you tried to get a shared_ptr from a raw ptr. PS有关is_block_type的错误是由shared_ptr引起的,可能是因为您试图从原始ptr中获取shared_ptr。 Read more about std::shared_ptr here .
在此处阅读有关 std::shared_ptr 的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.