4

I am using casablanca library to serialize json values.

I tried making a conversion to std::string using typedef std::wstring string_t and this to convert from wstring to string. It's compiling fine but the program just crashes when executing the return line.

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;
}

I can't understand why this is crashing. Below is the line calling that function.

std::string s = getString();

The program triggered a break point here at line free(_Ptr) in a file called xdebug. I don't really understand what it's saying here. Hope this helps clarify things for you.

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);
        }
    }

Thanks!

Community
  • 1
  • 1
XTT
  • 177
  • 1
  • 3
  • 13
  • 1
    Did you try stepping through the code to see where it crashes? – NathanOliver Jan 21 '16 at 19:45
  • @NathanOliver Hi did my edit help? it crash on the return line. But I don't know what's going on behind it. – XTT Jan 21 '16 at 20:01
  • Take out the json stuff out of that function and just hard-code a string literal, maybe `L"abc123"` and assign it to `outputString`. Does the function crash? – PaulMcKenzie Jan 21 '16 at 20:06
  • @PaulMcKenzie I just tried that. It crashed at the same place. – XTT Jan 21 '16 at 20:19
  • @XTT Ignore the compilation errors, but I just tried this code in Visual Studio 2015: http://ideone.com/1STzFR and the program works correctly. Maybe you should tell us exactly what version of Visual Studio you're using? Better yet, create a brand new console project, copy and paste the *exact* code you see at the link, build, and run. If it runs, then there is something wrong with your json Visual Studio project. Maybe a mismatched runtime library or something along those lines... – PaulMcKenzie Jan 21 '16 at 20:31
  • You almost certainly have heap corruption. The problem is probably elsewhere, and happened earlier in your application. – Collin Dauphinee Jan 21 '16 at 20:49
  • @XTT I was also going to mention heap corruption, especially if that simple example I gave works correctly. – PaulMcKenzie Jan 21 '16 at 20:50

1 Answers1

18

In the C++ REST SDK there is a function to convert utility::string_t into a utf8 std::string: utility::conversions::to_utf8string

reference documentation

roschuma
  • 536
  • 4
  • 7
  • very strange. there isn't a single function that takes utility::string_t and outputs std::string. – thang Feb 26 '23 at 05:49