Crash inside http_client constructor (Casablanca SDK)

不想你离开。 提交于 2021-01-29 05:32:17

问题


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.imgur.com/lBm0Hv7.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!


回答1:


I've experienced a similar issue on ubuntu. It works in an empty project, but crashes randomly when put into an existing large project, complaining memory corruptions.

Turns out that the existing project loaded a proprietary library, which is using cpprestsdk (casablanca) internally. Even cpprestsdk is static linked, its symbols are still exported as Weak Symbols. So either my code crashes, or the proprietary library crashes.

Ideally, my project can be divided into several libraries, and load them with RTLD_LOCAL to avoid symbol clashes. But the proprietary library in my project only accept RTLD_GLOBAL, otherwise it crashes... So the import order and flags become important:

dlopen("my-lib-uses-cpprest", RTLD_LOCAL); //To avoid polluting the global
dlopen("proprietary-lib-with-built-in-cpprest", RTLD_GLOBAL); //In my case, this lib must be global
dlopen("another-lib-uses-cpprest", RTLD_DEEPBIND); //To avoid being affected by global

"it will probably never concern anyone."

I agree with that.




回答2:


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.



来源:https://stackoverflow.com/questions/56115040/crash-inside-http-client-constructor-casablanca-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!