Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;std::vector::clear()崩溃_C++_Stdvector - Fatal编程技术网

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

C++ c++;std::vector::clear()崩溃,c++,stdvector,C++,Stdvector,我有一个程序,其中我有一个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; }

我有一个程序,其中我有一个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;
};
这在调试中可以正常工作,但在发布时出现“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++
Test2.exe!标准::向量::擦除
(std::_Vector_const_iterator)
_第一个参数={m_x=15 m_y=7},
标准::_向量_常量_迭代器
αLaSTiARG= {Myx=15 My y=8 }行1037 +0xe字节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;
}
BTNode::Status GoToDestBehavior::Update()
{
BTEntityData::Node*Node=m_dataRef->m_bTree.GetNode(m_索引);
if(node->m_state==BTNode::state_READY)
{
BehaviorTree::RequestDeferredAction(批处理::PATHFIND,m_dataRef->m_entityID);
返回BTNode::STATE\u RUNNING;
}
else if(node->m_state==BTNode::state_RUNNING)
{
std::vector path=m_dataRef->m_blackboard.GetPath();
EntitySystem::Entity*Entity=EntitySystem::GetEntity(m_dataRef->m_entityID);
断言(实体!=NULL,“无效实体”\n);
断言(实体->HasComponent(组件::PHYSICS\u COMP),“关联实体必须有物理组件才能移动\n”);
int phyIndex=entity->GetComponentIndex(组件::物理组件);
PhysicSystem::PhysicData*PhysicData=PhysicSystem::GetComponent(phyIndex);
断言(physicdata!=NULL,“无效的物理数据\n”);
//路径为空,请完成
if(path.size()==0)
{
physicdata->m_dir=Direction::NONE;//停止,因为我们在这里
返回BTNode::STATE\u SUCCESS;
}
//删除最后一个元素,如果我们在它
//LogFmt(“向量%d的大小”,path.Size());
Vector2 last=path.back();
如果(last.x()==physicdata->m_posX&&last.y()==physicdata->m_posY)
{
path.pop_back();
}
//路径的最后一个节点已被截断
if(path.size()==0)
{
physicdata->m_dir=Direction::NONE;//停止,因为我们在这里
m_dataRef->m_blackboard.ClearPath();
返回BTNode::STATE\u SUCCESS;
}
Vector2步骤=path.back();
physicdata->m_dir=Direction::vectordirection(physicdata->m_posX,physicdata->m_posY,step.x(),step.y());
如果(PhysicData->m_dir==方向::无)
{
m_dataRef->m_blackboard.SetPath(路径);
返回BTNode::STATE\u FAIL;
}
m_dataRef->m_blackboard.SetPath(路径);
返回BTNode::STATE\u RUNNING;
}
返回BTNode::STATE\u错误;
}
我不知道它为什么会这样。我在网上发现的大多数类似问题都有在空数组上调用clear的问题,但我对此有防范措施,所以这不应该是问题所在

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

我对这段代码了解得太多了,可能遗漏了一些明显的东西。我们将非常感谢您的帮助

提前感谢,,
Jaime

在任何类型的空容器上调用
clear
都可以

使用我的心理调试技巧,我确定在代码中,您没有向我们显示您正在访问实际上不存在的
向量的元素(可能是在插入它们之前,可能是使用
操作符[]
)。通常通过
调整大小
推回
插入
来创建元素


另一种可能是程序中的某个地方出现了另一个内存损坏。

在任何类型的空容器上调用
clear
都可以

使用我的心理调试技巧,我确定在代码中,您没有向我们显示您正在访问实际上不存在的
向量的元素(可能是在插入它们之前,可能是使用
操作符[]
)。通常通过
调整大小
推回
插入
来创建元素


另一种可能是程序中的某个地方出现了另一个内存损坏。

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


如果您有一个空列表,并且在其中调用了.clear(),并且发生了崩溃,那么您很有可能弄乱了内部跟踪内存,正如Mark在回答中提到的那样。寻找一个最有可能的罪魁祸首是您正在对包含类之类的内容进行内存清理的地方。

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


如果您有一个空列表,并且在其中调用了.clear(),并且发生了崩溃,那么您很有可能弄乱了内部跟踪内存,正如Mark在回答中提到的那样。寻找一个地方,其中最有可能的罪魁祸首是对包含类等的类进行内存清除。

在空向量上调用
clear()
是完全正确的,问题是在其他地方,也就是说,在执行代码之前,程序很可能有未定义的行为
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;
}