[英]C++ std::vector::clear() crash
I've got a program where I have a std::vector as a member of a class:我有一个程序,其中我有一个 std::vector 作为类的成员:
class Blackboard
{
public:
inline std::vector<Vector2<int> > GetPath()
{ return m_path; }
inline void SetPath(std::vector<Vector2<int> > path)
{ m_path = path; }
inline void ClearPath()
{ if(m_path.size() > 0) m_path.clear(); }
private:
std::vector<Vector2<int>> m_path;
};
Where the Vector2 class is defined as:其中 Vector2 类定义为:
template <class T>
class Vector2
{
private:
T m_x;
T m_y;
public:
Vector2(void)
{ m_x = 0; m_y = 0;}
Vector2(T x, T y)
{ m_x = x; m_y = y;}
~Vector2(void)
{ }
inline T x() const
{ return m_x; }
inline T y() const
{ return m_y; }
// ...
};
And at some point I call:在某些时候我打电话给:
m_blackboard.ClearPath();
This works fine in debug, but crashes in release with the "Microsoft Visual Studio C Runtime Library has detected a fatal error in Test2.exe."这在调试中运行良好,但在发布时崩溃并显示“Microsoft Visual Studio C 运行时库在 Test2.exe 中检测到致命错误”。 message.信息。
The call stack, at the last point where I can still see shows that:调用堆栈,在我仍然可以看到的最后一点显示:
Test2.exe!std::vector<RBT::Vector2<int>,
std::allocator<RBT::Vector2<int> > >::erase
(std::_Vector_const_iterator<RBT::Vector2<int>,
std::allocator<RBT::Vector2<int> > >
_First_arg={m_x=15 m_y=7 },
std::_Vector_const_iterator<RBT::Vector2<int>,
std::allocator<RBT::Vector2<int> > >
_Last_arg={m_x=15 m_y=8 }) Line 1037 + 0xe bytes C++
Here is where I'm calling the code that ends up crashing:这是我调用最终崩溃的代码的地方:
BTNode::Status GoToDestBehavior::Update()
{
BTEntityData::Node* node = m_dataRef->m_bTree.GetNode(m_index);
if(node->m_state == BTNode::STATE_READY)
{
BehaviorTree::RequestDeferredAction(Batch::PATHFIND, m_dataRef->m_entityID);
return BTNode::STATE_RUNNING;
}
else if(node->m_state == BTNode::STATE_RUNNING)
{
std::vector<Vector2<int>> path = m_dataRef->m_blackboard.GetPath();
EntitySystem::Entity* entity = EntitySystem::GetEntity(m_dataRef->m_entityID);
Assert(entity != NULL, "Invalid entity\n");
Assert(entity->HasComponent(Component::PHYSICS_COMP), "Associated entity must have physics component to move\n");
int phyIndex = entity->GetComponentIndex(Component::PHYSICS_COMP);
PhysicsSystem::PhysicsData * physicsData = PhysicsSystem::GetComponent(phyIndex);
Assert(physicsData != NULL, "Invalid physics data\n");
// Path is empty, so finish
if(path.size() == 0)
{
physicsData->m_dir = Direction::NONE; // Stop because we are here
return BTNode::STATE_SUCCESS;
}
// Remove last element if we are at it
//LogFmt("Size of vector %d\n", path.size());
Vector2<int> last = path.back();
if(last.x() == physicsData->m_posX && last.y() == physicsData->m_posY)
{
path.pop_back();
}
// Last node of the path has been transversed
if(path.size() == 0)
{
physicsData->m_dir = Direction::NONE; // Stop because we are here
m_dataRef->m_blackboard.ClearPath();
return BTNode::STATE_SUCCESS;
}
Vector2<int> step = path.back();
physicsData->m_dir = Direction::VectorToDirection(physicsData->m_posX, physicsData->m_posY, step.x(), step.y());
if(physicsData->m_dir == Direction::NONE)
{
m_dataRef->m_blackboard.SetPath(path);
return BTNode::STATE_FAIL;
}
m_dataRef->m_blackboard.SetPath(path);
return BTNode::STATE_RUNNING;
}
return BTNode::STATE_ERROR;
}
I don't know why it's behaving like this.我不知道为什么它的行为是这样的。 Most similar issues I've found online have the problem of calling clear on an empty array, but I have a guard against that, so it shouldn't be the issue.我在网上发现的大多数类似问题都有在空数组上调用 clear 的问题,但我对此有所防范,所以这应该不是问题。
The other thing I can think of is my Vector2 class requiring some kind of copy constructor or something for when I add elements to the vector, but in the end it's just 2 ints, so I don't know why that might be failing.我能想到的另一件事是我的 Vector2 类需要某种复制构造函数或在我向向量添加元素时使用的东西,但最终它只是 2 个整数,所以我不知道为什么这可能会失败。
I've been over this code too much and might be missing something obvious.我已经过多地研究了这段代码,可能会遗漏一些明显的东西。
It's perfectly fine to call clear
on an empty container of any sort.在任何类型的空容器上调用clear
非常好。
Using my psychic debugging skills, I have determined that in code you aren't showing us you're accessing elements of the vector
that don't actually exist (possibly before you inserted them, and probably with operator[]
).使用我的心灵调试技巧,我已经确定在代码中您没有向我们展示您正在访问实际上并不存在的vector
元素(可能在您插入它们之前,并且可能使用operator[]
)。 Usually element creation is done through resize
, push_back
, or insert
.通常元素创建是通过resize
、 push_back
或insert
。
The other possibility is that you have another memory corruption somewhere in your program.另一种可能性是您的程序中某处存在另一个内存损坏。
I found an issue I had due to a change in data format.我发现了由于数据格式更改而导致的问题。 The std::list I was using changed from a pointer to a list to directly the list.我使用的 std::list 从指向列表的指针更改为直接列表。 This started causing all sorts of errors that checking for the size of the list did not solve and were caused by a ZeroMemory()/memset() call that wiped out all of the tracking data of the list, since it was now part of the class instead of a pointer to the list.这开始导致检查列表大小无法解决的各种错误,并且是由 ZeroMemory()/memset() 调用引起的,该调用清除了列表的所有跟踪数据,因为它现在是列表的一部分类而不是指向列表的指针。
If you have an empty list and call .clear() on it with a crash, chances are you have messed up the internal tracking memory as mentioned by Mark in his answer.如果您有一个空列表并在崩溃时调用 .clear() ,那么您可能已经搞砸了 Mark 在他的回答中提到的内部跟踪内存。 Look for a place where you are doing memory clearing on containing classes and the like as the most likely culprits.寻找一个您正在清除包含类等的内存的地方,这些类最有可能是罪魁祸首。
I know it's been 8 years, but I thought too I had this problem when I was destroying an empty bst into which my code was sending a nullptr value to the __p variable in the implementation of "new_allocator.h".我知道已经 8 年了,但是当我销毁一个空的 bst 时我也遇到了这个问题,我的代码在“new_allocator.h”的实现中向 __p 变量发送了一个 nullptr 值。 This __p is needed to never be null, as mentioned in the file itself!正如文件本身所提到的,这个 __p 永远不能为空!
// __p is not permitted to be a null pointer. // __p 不允许是空指针。
The solution is not sending anything if you don't have something to send, basically.基本上,如果您没有要发送的东西,解决方案就是不发送任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.