簡體   English   中英

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類是從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_ptrtypedef std::shared_ptr<PlayerCharacter> character_ptr;

遵循David Schwartz和The Badger的建議,我確實重建了該項目,並且成功了。 可能是該文件未使用常規版本重新編譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM