我有一个用Visual C ++编写的应用程序,我在这个代码片段中遇到了访问冲突错误:
game_object_ptr GameObjectFactory::createGameObject(const int& id) {
game_object_ptr fullObj;
if(RANGE_PLAYERS_MIN <= id && RANGE_PLAYERS_MAX >= id) {
fullObj = game_object_ptr(new PlayerCharacter());
}
if(fullObj) {
return fullObj; // Crashes here...
}
return nullptr;
}
game_object_ptr
的定义如下:typedef std::shared_ptr<GameObject> game_object_ptr;
PlayerCharacter
类派生自GameObject
。
调用堆栈如下:
msvcr110d.dll!operator delete(void * pUserData) Line 52 C++
MyApp.exe!GameObject::`scalar deleting destructor'(unsigned int) C++
MyApp.exe!std::_Ref_count<GameObject>::_Destroy() Line 161 C++
MyApp.exe!std::_Ref_count_base::_Decref() Line 120 C++
MyApp.exe!std::_Ptr_base<GameObject>::_Decref() Line 347 C++
MyApp.exe!std::shared_ptr<GameObject>::~shared_ptr<GameObject>() Line 624 C++
MyApp.exe!GameObjectFactory::createGameObject(const int & id) Line 44 C++
(...)
所以看起来崩溃发生在删除GameObject
对象时,建议它被删除两次,但我不知道它为什么会被删除,如果 - 据我所知 - 参考共享资源的计数不应该降为0,因为我在调用createGameObject()
函数时进一步传递所有权:
account->m_characterInGame = std::dynamic_pointer_cast<PlayerCharacter>(GameObjectFactory::createGameObject(characterInfo->m_typeId));
有没有人知道这里发生了什么?也许我错过了一些明显的东西。
编辑:
m_characterInGame
的定义如下:
character_ptr m_characterInGame;
character_ptr
:
typedef std::shared_ptr<PlayerCharacter> character_ptr;
答案 0 :(得分:0)
根据David Schwartz和The Badger的建议,我确实重建了这个项目,这就行了。可能没有使用正常构建重新编译该文件。