我再次遇到內存分配問題,我找不到原因。std :: string構造函數中的內存分配/堆損壞
當我運行在調試模式下我收到以下錯誤消息的程序(我試着將它儘可能準確地翻譯):
Windows已經引發了LogoColorDetector.exe一個斷點。這可以通過堆損壞這表明在LogoColorDetector.exe還是有問題的加載的DLL的一個[...]引起
當我調試我發現,問題似乎在以下行發生程序:
const std::string& support::Image::getFilepath() const
{
return this->_filePath;
}
我已經檢查如果在imgTrain [j]的圖像 - 物體具有正確_filePath字符串: -
std::string tmp = imgTrain2[j]->getFilepath();
的getFilepath()函數是如下實施。所以我認爲這個問題在別的地方。有趣的是,包含有問題的行的函數似乎工作。這只是我第二次調用失敗的函數,這表明問題不在函數本身中。我不分配任何內存我也不除了什麼是可能通過的std :: string
間接完成萬一功能刪除任何它是一個任何人的幫助,這裏的堆棧跟蹤:
msvcr100d.dll!_heap_alloc_base(unsigned int size) Zeile 55 C
msvcr100d.dll!_heap_alloc_dbg_impl(unsigned int nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Zeile 431 + 0x9 Bytes C++
msvcr100d.dll!_nh_malloc_dbg_impl(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Zeile 239 + 0x19 Bytes C++
msvcr100d.dll!_nh_malloc_dbg(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine) Zeile 302 + 0x1d Bytes C++
msvcr100d.dll!malloc(unsigned int nSize) Zeile 56 + 0x15 Bytes C++
msvcr100d.dll!operator new(unsigned int size) Zeile 59 + 0x9 Bytes C++
LogoColorDetector.exe!std::_Allocate<char>(unsigned int _Count, char * __formal) Zeile 36 + 0xf Bytes C++
LogoColorDetector.exe!std::allocator<char>::allocate(unsigned int _Count) Zeile 187 + 0xb Bytes C++
LogoColorDetector.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Copy(unsigned int _Newsize, unsigned int _Oldlen) Zeile 1933 + 0x12 Bytes C++
LogoColorDetector.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Grow(unsigned int _Newsize, bool _Trim) Zeile 1963 + 0x13 Bytes C++
LogoColorDetector.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & _Right, unsigned int _Roff, unsigned int _Count) Zeile 902 + 0xe Bytes C++
LogoColorDetector.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> >(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & _Right) Zeile 546 C++
LogoColorDetector.exe!compareClasses(support::ImageCollection * coll, support::ImageClass * cl1, support::ImageClass * cl2, float * mean, float * var) Zeile 111 + 0x22 Bytes C++
有沒有人有什麼可能會導致此問題的想法?
感謝您的幫助。
- 編輯 -
試圖與視覺檢漏儀的建議。它不會顯示任何內容,直到上面提到的錯誤消息彈出並說內存在釋放後被修改。有沒有辦法找出什麼對象與內存地址相關 - 內存轉儲似乎不是很有幫助。
爲了使事情變得更加myterious我嘗試添加以下行:
std::string tmp = imgTrain2[j]->getFilepath();
std::string t2Path = imgTrain2[j]->getFilepath();
現在第一線的正確執行和第二線失敗。
怎麼樣getFilepath()返回一個副本,而不是參考? 'const std :: string support :: Image :: getFilepath()const' – karlphillip