I just met a weird bug, where the code works in Debug mode but does not work in Release mode. After miserable debugging, I found the following function is called (source: xstring), and the program jumps into the if block _Mypair._Myval2._Large_string_engaged()
. Then, I believe some important object is destroyed and the whole program crashed. When I change the length of the long string, the error disappears. I wonder why would this happen, and how I canuse long string in Release mode. The string is about 20 letters.
void _Tidy_deallocate() noexcept { // initialize buffer, deallocating any storage
_Mypair._Myval2._Orphan_all();
if (_Mypair._Myval2._Large_string_engaged()) {
const pointer _Ptr = _Mypair._Myval2._Bx._Ptr;
auto& _Al = _Getal();
_Destroy_in_place(_Mypair._Myval2._Bx._Ptr);
_Al.deallocate(_Ptr, _Mypair._Myval2._Myres + 1);
}
_Mypair._Myval2._Mysize = 0;
_Mypair._Myval2._Myres = _BUF_SIZE - 1;
// the _Traits::assign is last so the codegen doesn't think the char write can alias this
_Traits::assign(_Mypair._Myval2._Bx._Buf[0], _Elem());
} void _Tidy_deallocate() noexcept { // initialize buffer, deallocating any storage
_Mypair._Myval2._Orphan_all();
if (_Mypair._Myval2._Large_string_engaged()) {
const pointer _Ptr = _Mypair._Myval2._Bx._Ptr;
auto& _Al = _Getal();
_Destroy_in_place(_Mypair._Myval2._Bx._Ptr);
_Al.deallocate(_Ptr, _Mypair._Myval2._Myres + 1);
}
_Mypair._Myval2._Mysize = 0;
_Mypair._Myval2._Myres = _BUF_SIZE - 1;
// the _Traits::assign is last so the codegen doesn't think the char write can alias this
_Traits::assign(_Mypair._Myval2._Bx._Buf[0], _Elem());
}
The programs works well in Debug mode because different memory function is called I think.
void __CRTDECL operator delete(void* const block) noexcept
{
#ifdef _DEBUG
_free_dbg(block, _UNKNOWN_BLOCK);
#else
free(block);
#endif
}
Thank you in advance!