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.
const std::string support::Image::getFilepath() const