c++ - Memory allocation / Heap corruption in std::string constructor -


i have trouble memory allocation again , can't figure out why.

when run program in debug mode recieve following error message (i tried translate accurately possible):

windows has triggered breakpoint in logocolordetector.exe. can caused heap corruption indicates problem in logocolordetector.exe or 1 of loaded dlls[...]

when debug program found problem seems occur in following line:

std::string tmp = imgtrain2[j]->getfilepath(); 

the getfilepath()-function implemented follows:

const std::string& support::image::getfilepath() const {     return this->_filepath; } 

i have checked if image-object @ imgtrain[j] has correct _filepath string. assume problem somewhere else. funny thing is, function contains problematic line seems work. it's second time call function fails indicate problem not in function itself. don't allocate memory nor delete in function except maybe done indirectly through std::string

in case anyone, here 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 have idea on cause this?

thanks help.

-- edit --

tried suggestion visual leak detector. doesn't show until moment above mentioned error message pops , says memory modified after has been freed. there way find out object associated memory address - memory dump doesn't seem helpful.

to make things more myterious tried adding following line:

std::string tmp = imgtrain2[j]->getfilepath(); std::string t2path = imgtrain2[j]->getfilepath(); 

now first line executed correctly , second line fails.

when runtime detects heap corruption heap already corrupted. means previous operation messed (e.g.: wrote beyond range of array, have corrupted pointers, etc..).

run program visual leak detector or tool can signal error @ exact point overwrite memory location shouldn't (note: still might not show error in code, example if corrupted pointer earlier, @ least give hint what corrupted).

update: addendum david's answer (sorry, doesn't allow long comments)

to finish analogy: there bug in program. when line containing bug executed, might create error. overwrites part of heap store information (an) allocated memory chunk(s). runtime doesn't recognise this, memory segment belongs process, should able write it. no failure. later (maybe in different part of app) try allocate new memory, new operator calls heap_alloc_ new memory chunk. code alloc walks through the chain of allocated memory chunks , founds garbage there. triggers failure let know bad going on. have find bug in code triggered error. without tool have check whole code hunting bug.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -