5

在我的申請,我得到這個錯誤:堆腐敗 - 「自由堆塊61af0f0修改的61af194在被釋放後的」 C++

HEAP[App.exe]: HEAP: Free Heap block 61af0f0 modified at 61af194 after it was freed 

下面是一個調用堆棧:

[email protected]() Unknown 
    [email protected]@24() Unknown 
    [email protected]() Unknown 
    [email protected]() Unknown 
    [email protected]@24() Unknown 
    [email protected]() Unknown 
> msvcr110d.dll!_heap_alloc_base(unsigned int size) Line 57 C 
    msvcr110d.dll!_heap_alloc_dbg_impl(unsigned int nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 431 C++ 
    msvcr110d.dll!_nh_malloc_dbg_impl(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 239 C++ 
    msvcr110d.dll!_nh_malloc_dbg(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine) Line 302 C++ 
    msvcr110d.dll!malloc(unsigned int nSize) Line 56 C++ 
    msvcr110d.dll!operator new(unsigned int size) Line 59 C++ 
    App.exe!std::_Allocate<char>(unsigned int _Count, char * __formal) Line 28 C++ 
    App.exe!std::allocator<char>::allocate(unsigned int _Count) Line 591 C++ 
    App.exe!std::basic_stringbuf<char,std::char_traits<char>,std::allocator<char> >::overflow(int _Meta) Line 152 C++ 
    msvcp110d.dll!std::basic_streambuf<char,std::char_traits<char> >::sputc(char _Ch) Line 196 C++ 
    msvcp110d.dll!std::ostreambuf_iterator<char,std::char_traits<char> >::operator=(char _Right) Line 634 C++ 
    msvcp110d.dll!std::num_put<char,std::ostreambuf_iterator<char,std::char_traits<char> > >::_Put(std::ostreambuf_iterator<char,std::char_traits<char> > _Dest, const char * _Ptr, unsigned int _Count) Line 1553 C++ 
    msvcp110d.dll!std::num_put<char,std::ostreambuf_iterator<char,std::char_traits<char> > >::_Iput(std::ostreambuf_iterator<char,std::char_traits<char> > _Dest, std::ios_base & _Iosbase, char _Fill, char * _Buf, unsigned int _Count) Line 1544 C++ 
    msvcp110d.dll!std::num_put<char,std::ostreambuf_iterator<char,std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char,std::char_traits<char> > _Dest, std::ios_base & _Iosbase, char _Fill, long _Val) Line 1216 C++ 
    msvcp110d.dll!std::num_put<char,std::ostreambuf_iterator<char,std::char_traits<char> > >::put(std::ostreambuf_iterator<char,std::char_traits<char> > _Dest, std::ios_base & _Iosbase, char _Fill, long _Val) Line 1137 C++ 
    msvcp110d.dll!std::basic_ostream<char,std::char_traits<char> >::operator<<(int _Val) Line 311 C++ 
    App.exe!TUtil::intToString(int val) Line 43 C++ 
    App.exe!TFontManager::getFont(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & filename, int size) Line 15 C++ 
    App.exe!TButton::draw() Line 55 C++ 
    App.exe!TWindow::draw() Line 203 C++ 
    App.exe!TGUIManager::drawObjects() Line 49 C++ 
    App.exe!TGameAppLayer::gameCycle() Line 456 C++ 
    App.exe!TGameAppLayer::mainLoop() Line 520 C++ 
    App.exe!wWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, wchar_t * lpCmdLine, int nCmdShow) Line 36 C++ 
    App.exe!__tmainCRTStartup() Line 528 C 
    App.exe!wWinMainCRTStartup() Line 377 C 
    [email protected]@12() Unknown 
    [email protected]() Unknown 
    [email protected]() Unknown 

所以從我所知道的情況來看,我得到的錯誤是由於已經釋放的內存塊被訪問(再次刪除)而導致的。這已經是第三天瞭解我的代碼究竟出了什麼問題。在此期間,我發現了一些我已經修復的小內存泄漏,現在Visual Leak Detector告訴我它沒有檢測到任何泄漏。

然而,堆腐敗的問題依然存在。

在使用「delete」操作符的地方我的代碼的每個地方,我首先檢查指針是否不是nullptr。如果沒有,我將其設置爲nullptr

if(m_pVar != nullptr) 
{ 
    delete m_pVar; 
    m_pVar = nullptr; 
} 

因此它似乎不應該有與釋放的內存不止一次同一塊有問題。

我試圖從這個調用堆棧中找出一些東西,但這是我想請求幫助的地方。在調用堆棧中,似乎有一個問題是string分配,但究竟是什麼意思?最後的MY函數被調用爲string TUtil::intToString(int val) Line 43,所以如果我告訴你,功能的身體可能會更容易:

std::string TUtil::intToString(int val) 
{ 
    std::ostringstream s; 
    s << val;     // Here's line 43 
    return s.str(); 
} 

有時調用堆棧是不同的,所以string TUtil::intToString(int val)功能甚至不存在它,但它始終與string的分配有關。

我希望我說的很清楚。如果您需要更多信息,請告訴我,我將在編輯時將其提供給此問題。

+3

您是否在Valgrind是選項的平臺上? –

+0

如果您正在使用linux,請嘗試使用Valgrind檢查您的應用程序 – Vargan

+2

您是否定義了複製構造函數和賦值運算符,或者顯式禁止複製具有成員'm_pVar'的類的實例? – hmjd

回答

9

So from what I know, the error I'm getting is caused by accessing (deleting again) block of memory which was already freed.

除非有什麼你知道,並沒有告訴我們,上述可能是一個紅鯡魚。錯誤可能是意味着你正在通過懸掛指針修改內存,或由於緩衝區溢出。

如果你做指針的副本(無論是明確的,或者沒有定義拷貝構造函數/賦值運算符),設置m_pVar = nullptr刪除時將提供對雙刪除沒有註冊就,更不用說其他類型的內存錯誤。

如果您通過檢查代碼找不到問題,您最好的選擇可能是像Valgrind或Purify這樣的工具。

+4

謝謝NPE的回答!抱歉有點遲到的回覆,但我仍然在根據你提供給我的提示找到一個問題,最後我感謝「Application Verifier」和「Debug Diagnostics Tool」程序。看來我一直在使用已經釋放的內存塊的指針。 –

2

在malloc期間發生崩潰是內存損壞的一個肯定跡象,它可能會或可能不會由於雙重刪除。腐敗發生在你的代碼的某個不同部分,不幸的是,這種影響波及了你的失敗代碼,這絕對是無辜的。如果可能,嘗試將您的應用程序移植到您可以運行valgrind的系統中

+0

謝謝Abhijit的回答。問題是使用指向已經釋放的內存塊的指針。 –