内存分配/堆损坏在std :: string构造函数 [英] Memory allocation / Heap corruption in std::string constructor

查看:1540
本文介绍了内存分配/堆损坏在std :: string构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在调试模式下运行程序时收到以下错误信息我尝试尽可能准确地翻译):



Windows在LogoColorDetector.exe中触发了一个断点。 [...]



当我调试程序时,我发现问题似乎发生在下面这行:

  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字节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字节C ++
msvcr100d.dll!_nh_malloc_dbg(unsigned int nSize,int nhFlag,int nBlockUse,const char * szFileName,int nLine)Zeile 302 + 0x1d字节C ++
msvcr100d.dll!malloc(unsigned int nSize)Zeile 56 + 0x15字节C ++
msvcr100d.dll!操作符new(unsigned int size)Zeile 59 + 0x9字节C ++
LogoColorDetector.exe!std: :_Allocate< char>(unsigned int _Count,char * __formal)Zeile 36 + 0xf字节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 Byte 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字节C ++

有没有人知道可能导致这种情况的原因?



感谢您的帮助。



- 编辑



使用Visual Leak Detector尝试建议。它不显示任何东西,直到上述提到的错误消息弹出并说,内存已被修改后,它已被释放。有没有办法找出什么对象是与内存地址相关联的 - 内存转储似乎不是非常有帮助。



为了使事情更加恶劣我试过添加以下行:

  std :: string tmp = imgTrain2 [j]  - > getFilepath 
std :: string t2Path = imgTrain2 [j] - > getFilepath();

现在第一行正确执行,第二行执行失败。

解决方案

当运行时检测到堆损坏时,堆已被损坏。这意味着以前的操作会把它搞砸(例如:你写了超出数组范围的东西,你的指针已损坏等等)。



运行你的程序与视觉泄漏检测器或任何工具,可以信号的错误,在你覆盖的内存位置的精确的点,你不应该是(注意:这仍然可能不会显示错误的代码,例如,如果你先前损坏了一个指针,



更新:David的回答的一些补充文件(对不起,SO不允许长评论)



要完成类比:您的程序中有一个错误。当包含错误的行被执行时,它可能会创建一个错误。它覆盖堆的一部分,您在此存储有关分配的内存块的信息。运行时无法识别这一点,该内存段属于你的进程,所以你应该能够写它。没有失败。现在以后(也许在你的应用程序的一个完全不同的部分),你尝试分配新的内存,新操作员调用heap_alloc_来获得一个新的内存块。 alloc的代码遍历分配的内存块的链,并发现一些垃圾。它触发一个失败,让你知道,一些真正糟糕的事情正在发生。现在你必须在你的代码中找到触发错误的错误。没有一些工具,你必须检查你的整个代码寻找一个错误。


I have some trouble with memory allocation again and I can't figure out why.

When I run the program in debug mode I recieve the following error message (I tried to translate it as accurately as possible):

Windows has triggered a breakpoint in LogoColorDetector.exe. This can be caused by heap corruption which indicates a problem in LogoColorDetector.exe or one of its loaded DLLs[...]

When I debug the program I found that the problem seems to occur in the following line:

std::string tmp = imgTrain2[j]->getFilepath();

The getFilepath()-Function is implemented as follows:

const std::string& support::Image::getFilepath() const
{
    return this->_filePath;
}

I have already checked if the Image-object at imgTrain[j] has a correct _filePath string. So I assume the problem is somewhere else. The funny thing is, that the function that contains the problematic line seems to work. It's only the second time I call the function where it fails which would indicate that the problem is not in the function itself. I don't allocate any memory nor do i delete anything in the function except what is maybe done indirectly through std::string

In case it is a help for anyone, here the stack trace:

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++

Does anyone have an idea on what could cause this?

Thanks for your help.

-- edit --

Tried the suggestion with Visual Leak Detector. It doesn't show anything until the moment the above mentioned error message pops up and says that memory was modified after it has been freed. Is there a way to find out what object was associated with the memory address - the memory dump doesn't seem to be very helpful.

To make things more myterious I tried adding the following line:

std::string tmp = imgTrain2[j]->getFilepath();
std::string t2Path = imgTrain2[j]->getFilepath();

Now the first line is executed correctly and the second line fails.

解决方案

When the runtime detects a heap corruption the heap is already corrupted. It means that a previous operation messed it up (e.g.: you wrote something beyond the range of an array, you have corrupted pointers, etc..).

Run you program with visual leak detector or any tool that can signal the error at the exact point where you overwrite a memory location you shouldn't be (Note: this still might not show the error in your code, for example if you corrupted a pointer earlier, but at least it will give you a hint what is corrupted).

Update: some addendum to David's answer (sorry, SO doesn't allow long comments)

To finish the analogy: there is a bug in your program. When the line containing the bug is executed, it might create an error. It overwrites a part of the heap where you store information about (an) allocated memory chunk(s). The runtime doesn't recognise this, that memory segment belongs to your process, so you should be able to write to it. No failure. Now later (maybe in a completely different part of your app) you try to allocate new memory, the new operator calls heap_alloc_ to get a new memory chunk. The code for alloc walks through the the chain of allocated memory chunks and founds some garbage there. It triggers a failure to let you know that something really bad is going on. Now you have to find the bug in your code that triggered the error. Without some tool you have to check your whole code hunting for a bug.

这篇关于内存分配/堆损坏在std :: string构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆