繁体   English   中英

C++ std::vector::clear() 崩溃

[英]C++ std::vector::clear() crash

我有一个程序,其中我有一个 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;
};

其中 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; }

    // ...
};

在某些时候我打电话给:

m_blackboard.ClearPath();

这在调试中运行良好,但在发布时崩溃并显示“Microsoft Visual Studio C 运行时库在 Test2.exe 中检测到致命错误”。 信息。

调用堆栈,在我仍然可以看到的最后一点显示:

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++

这是我调用最终崩溃的代码的地方:

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;
}

我不知道为什么它的行为是这样的。 我在网上发现的大多数类似问题都有在空数组上调用 clear 的问题,但我对此有所防范,所以这应该不是问题。

我能想到的另一件事是我的 Vector2 类需要某种复制构造函数或在我向向量添加元素时使用的东西,但最终它只是 2 个整数,所以我不知道为什么这可能会失败。

我已经过多地研究了这段代码,可能会遗漏一些明显的东西。

在任何类型的空容器上调用clear非常好。

使用我的心灵调试技巧,我已经确定在代码中您没有向我们展示您正在访问实际上并不存在的vector元素(可能在您插入它们之前,并且可能使用operator[] )。 通常元素创建是通过resizepush_backinsert

另一种可能性是您的程序中某处存在另一个内存损坏。

我发现了由于数据格式更改而导致的问题。 我使用的 std::list 从指向列表的指针更改为直接列表。 这开始导致检查列表大小无法解决的各种错误,并且是由 ZeroMemory()/memset() 调用引起的,该调用清除了列表的所有跟踪数据,因为它现在是列表的一部分类而不是指向列表的指针。

如果您有一个空列表并在崩溃时调用 .clear() ,那么您可能已经搞砸了 Mark 在他的回答中提到的内部跟踪内存。 寻找一个您正在清除包含类等的内存的地方,这些类最有可能是罪魁祸首。

我知道已经 8 年了,但是当我销毁一个空的 bst 时我也遇到了这个问题,我的代码在“new_allocator.h”的实现中向 __p 变量发送了一个 nullptr 值。 正如文件本身所提到的,这个 __p 永远不能为空!

// __p 不允许是空指针。

基本上,如果您没有要发送的东西,解决方案就是不发送任何东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM