0

我有一个程序,我有一个 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 个整数,所以我不知道为什么这可能会失败。

我对这段代码的了解太多了,可能会遗漏一些明显的东西。

4

3 回答 3

4

clear调用任何类型的空容器都很好。

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

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

于 2013-04-09T20:13:19.533 回答
0

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

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

于 2015-04-22T17:48:18.733 回答
0

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

// __p 不允许为空指针。

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

于 2021-07-29T17:36:04.343 回答