我在内存分配方面再次遇到了一些麻烦,但我不明白为什么。
当我在调试模式下运行程序时,我收到以下错误消息(我尝试尽可能准确地翻译它):
Windows 已触发 LogoColorDetector.exe 中的断点。这可能是由堆损坏引起的,这表明 LogoColorDetector.exe 或其加载的 DLL 之一存在问题[...]
当我调试程序时,我发现问题似乎出现在以下行中:
std::string tmp = imgTrain2[j]->getFilepath();
getFilepath() 函数的实现如下:
const std::string& support::Image::getFilepath() const
{
return this->_filePath;
}
我已经检查过 imgTrain[j] 处的图像对象是否具有正确的 _filePath 字符串。所以我认为问题出在其他地方。有趣的是,包含有问题的行的函数似乎可以工作。只有第二次调用该函数时才会失败,这表明问题不在于函数本身。我不分配任何内存,也不删除函数中的任何内容,除了通过 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++
有谁知道什么可能导致这种情况?
感谢您的帮助。
- 编辑 -
使用视觉检漏仪尝试了建议。它不会显示任何内容,直到弹出上述错误消息并表示内存在释放后被修改。有没有办法找出与内存地址关联的对象 - 内存转储似乎不是很有帮助。
为了让事情变得更加神秘,我尝试添加以下行:
std::string tmp = imgTrain2[j]->getFilepath();
std::string t2Path = imgTrain2[j]->getFilepath();
现在第一行正确执行,第二行失败。