0

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!

  • 4
    Manufacture a simple, [mcve] and post it in your question. Something we can copy/paste/compile/run, *without alteration*, and reproduce your problem, would fit the bill nicely. And fyi, 20 letters is *not* very much and probably has nothing to do with the root cause of this problem. You should also include the specific version of vc++ you're using. – WhozCraig Feb 17 '21 at 10:40
  • 1
    The relevant code is your own, not the library. Follow the callstack until you reach your own code and start digging there. (The primary suspect is attempting to use a destroyed or otherwise invalid object.) – molbdnilo Feb 17 '21 at 10:43
  • 1
    My guess: Use-after-free on a `std::string` or something like it. – tadman Feb 17 '21 at 10:48

0 Answers0