C++,从实用程序:string_t到std :: string的转换在返回时崩溃

XTT*_*XTT 1 c++ string json type-conversion casablanca

我正在使用casablanca库来序列化json值.

我试图使一个转换的std :: string使用typedef std::wstring string_t这个从wstring的字符串转换.它编译正常,但程序在执行返回行时崩溃.

std::string getString()
{
    web::json::value cvalue;
    /* ----- code ----- */
    typedef std::wstring string_t;
    string_t outputString = cvalue.serialize();

    typedef std::codecvt_utf8<wchar_t> convert_type;
    std::wstring_convert<convert_type, wchar_t> converter;
    std::string converted_str = converter.to_bytes(outputString);

    return converted_str;
}
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么会崩溃.下面是调用该函数的行.

std::string s = getString();
Run Code Online (Sandbox Code Playgroud)

该程序free(_Ptr)在一个名为xdebug的文件中触发了一个断点.我真的不明白这里说的是什么.希望这有助于为您澄清事情.

template<class _Ty>
    void __CLRCALL_OR_CDECL _DebugHeapDelete(_Ty *_Ptr)
    {   // delete from the debug CRT heap even if operator delete exists
    if (_Ptr != 0)
        {   // worth deleting
        _Ptr->~_Ty();
        // delete as _NORMAL_BLOCK, not _CRT_BLOCK, since we might have
        // facets allocated by normal new.
        free(_Ptr);
        }
    }
Run Code Online (Sandbox Code Playgroud)

谢谢!

ros*_*uma 9

在C++ REST SDK中有一个转换utility::string_t为utf8 的函数std::string:utility::conversions::to_utf8string

参考文件