shared_ptr - 操作员删除中的访问冲突

shared_ptr - access violation in operator delete

我有一个用 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 class 派生自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_ptrtypedef std::shared_ptr<PlayerCharacter> character_ptr;

听从 David Schwartz 和 The Badger 的建议,我确实重建了项目并且成功了。可能,该文件未使用正常构建重新编译。