簡體   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