Search code examples
restc++11casablanca

Crash inside http_client constructor (Casablanca SDK)


I'm trying to use Casablanca to consume a REST api. I've been following the microsoft tutorial, how ever i'm getting a crash and I cannot figure it out.

I'm using visual studio 2017 with C++11

I've codded a function GetRequest() that do work when used in a new empty project, but when I try to use it on my Project (Very big project with millions of code lines). I'm crashing in the constructor of http_client, in the file xmemory0 line 118.

const uintptr_t _Ptr_container = _Ptr_user[-1];

This is a link to the callstack : https://i.sstatic.net/8aUef.png

void RestManager::GetRequest()
{
    auto fileStream = std::make_shared<ostream>();

    // Open stream to output file.
    pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
    {
        *fileStream = outFile;

        // Create http_client to send the request.
        http_client client(U("XXX/XXX.svc/"));

        // Build request URI and start the request.
        uri_builder builder(U("/IsLive"));
        builder.append_query(U("q"), U("cpprestsdk github"));
        return client.request(methods::GET, builder.to_string());
    })

        // Handle response headers arriving.
        .then([=](http_response response)
    {
        printf("Received response status code:%u\n", response.status_code());

        // Write response body into the file.
    return response.body().read_to_end(fileStream->streambuf());
    })    

        // Close the file stream.
        .then([=](size_t)
    {
        return fileStream->close();
    });

    // Wait for all the outstanding I/O to complete and handle any exceptions
    try
    {
        requestTask.wait();
    }
    catch (const std::exception &e)
    {
        printf("Error exception:%s\n", e.what());
    }

}

EDIT : I just want to add that the http_client constructor is the issue. It always crash inside it no matter what I send as parameter.

The wierd thing is that it's not crashing when i just make a main() that call this function. I guess it must be due to some memory issues, however I have no idea how could I debug that. Does anyone would have an idea about it?

Thanks and have a great day!


Solution

  • I guess this issues was very specific, and it will probably never concern anyone, but still I'm going to update on everything I found out about it.

    On this project, we are using custom allocator, if i'm not wrong, it's not possible to give our custom allocator to this lib, which result to many random crash.

    A good option to fix it would be to use the static version to this lib, however, since we are using a lot of dynamic lib, this option wasn't possible for us.

    If you are on my case, I would advice to use the libcurl and rapidjson, it's a bit harder to use, but you can achieve the same goal.