gpt4 book ai didi

c++ - 调用 delete 导致 C++ 程序卡住

转载 作者:行者123 更新时间:2023-11-28 03:06:56 25 4
gpt4 key购买 nike

void FactorySystem::deleteObjectsToBeDeleted()
{
//Delete all objects in the ObjectsToBeDeleted list
std::vector<unsigned>::iterator it = objectsToBeDeleted.begin();
for(unsigned i = 0; i < objectsToBeDeleted.size(); i++)
{
GameObjectIDMapType::iterator it = gameObjectIDMap.find(objectsToBeDeleted[i]);
if(it == gameObjectIDMap.end())
std::cout << "Bad memory or double deletion error!" << std::endl;
else
{
//Delete it and remove its entry in the ID map
std::cout << (it->second->GetID()) << std::endl;
GameObject *object = it->second;
delete object;
gameObjectIDMap.erase(it);
}
}
//All objects to be delete have been deleted
objectsToBeDeleted.clear();
}

在我调用 delete object 的那一行,程序永远挂起,我不明白为什么。我用谷歌搜索了这个问题,好像从来没有人遇到过这个问题。我一定是在做坏事,但我不知道它可能是什么。有任何想法吗?谢谢。

编辑:

我被要求展示 GameObject 的析构函数,这里是:

GameObject::~GameObject()
{
//Delete each component using the component's virtual destructor
//takes care of all resources and memory.
for( ComponentMapIt it = componentMap.begin();it!=componentMap.end();++it)
delete it->second;
}

另一件事是,我进入了反汇编并能够加紧直到:

00488DA9  call        GameObject::`scalar deleting destructor' (044055Fh)  

然后它就挂起,没有其他任何事情发生。

编辑:这是我的菜鸟错误。出于某种原因,我无法进入 delete 调用,所以我假设它在那里,但在其中放置一个断点允许我进入它。谢谢大家,您的所有建议都非常有帮助。

void PhysicsManager::Unregister(RigidBody *Obj)
{
std::list<RigidBody*>::iterator it = MasterList.begin();

while(it != MasterList.end())
{
if(*it == Obj)
{
MasterList.erase(it);
return;
}
}
}

最佳答案

void PhysicsManager::Unregister(RigidBody *Obj)
{
std::list<RigidBody*>::iterator it = MasterList.begin();

while(it != MasterList.end())
{
if(*it == Obj)
{
MasterList.erase(it);
return;
}
}
}

it 是恒定不变的:一旦循环开始并且 *it != Obj 循环将永远持续

关于c++ - 调用 delete 导致 C++ 程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19442937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com