building cpprest with gcc 5.1

Jul 28, 2015 at 7:47 PM
Hi,

Has anyone tried to build cpprest on Linux with gcc 5? I was able to build it but I get some segment violation when running a test app.
Here is what I did:
  1. I installed on my Mint box (64 bit) gcc 5.1 and made it the default compiler.
  2. I got boost 1.58, compiled it and deploy it.
  3. I built the cpprest sdk following the steps in the docs but I also run at the end 'make install'. This in fact worked and I ended up with libcpprest.so in /usr/local/lib.
  4. So up to this point, all seems ok. However when I try to run a test app (do an http request), I got a segment fault but not always!
Here is the code:
  auto lf0 = [&]() -> void
  {
    web::http::http_request msg(web::http::methods::GET);
    concurrency::streams::container_buffer<std::vector<uint8_t>> buf;  //use vector as internal storage.
    msg.set_response_stream(buf.create_ostream());
    msg.set_progress_handler([](web::http::message_direction::direction wSens, utility::size64_t dataRead)
    {
      cout << "got : " << dataRead << " bytes " << (wSens == web::http::message_direction::direction::upload ? "up" : "down") << endl;
    });
    web::http::client::http_client client("myurlfordownload");
    try
    {
      client.request(msg).then([](web::http::http_response response)
      {
        cout << "Http ready to download, http respose code " << response.status_code() << endl;
        return response.content_ready(); // read all the body data.
      }).wait();
      auto result = std::move(buf.collection());
      cout << "Got " << result.size() << " bytes" << endl;
    }
    catch (web::http::http_exception& ex)
    {
      cout << ex.error_code().value() << " " << ex.what() << endl;
    }
    catch (...)
    {
      cout << "Unknown exception!" << endl;
    }
  };
This code will fail with a segmentation violation. However, if I take out the progress event code, it works ok.

Thanks,

GT
Jul 28, 2015 at 8:24 PM
Can you compile the above in debug mode and attach GDB to find where the segfault is happening?
Jul 28, 2015 at 10:05 PM
Here is the stack trace:
#0 ??   std::_Function_handler<void(web::http::message_direction::direction, long unsigned int), TestCasablanca()::<lambda(std::string)>::<lambda(web::http::message_direction::direction, utility::size64_t)> >::_M_invoke(const std::_Any_data &, <unknown type in /home/gheorghe/bsoft_svn/Tests/BSoftTests/bin/Debug/BSoftTests, CU 0x426718, DIE 0x4d2b35>, <unknown type in /home/gheorghe/bsoft_svn/Tests/BSoftTests/bin/Debug/BSoftTests, CU 0x426718, DIE 0x4d2b3a>) (__functor=..., __args#0=<unknown type in /home/gheorghe/bsoft_svn/Tests/BSoftTests/bin/Debug/BSoftTests, CU 0x426718, DIE 0x4d2b35>, __args#1=<unknown type in /home/gheorghe/bsoft_svn/Tests/BSoftTests/bin/Debug/BSoftTests, CU 0x426718, DIE 0x4d2b3a>) (/usr/include/c++/5/functional:1871)
#1 0x7ffff6a2a7e1   web::http::client::details::asio_context::handle_write_body(boost::system::error_code const&) () (/usr/local/lib/libcpprest.so.2.6:??)
#2 0x7ffff6a2aae7   web::http::client::details::asio_context::handle_write_large_body(boost::system::error_code const&) () (/usr/local/lib/libcpprest.so.2.6:??)
#3 0x7ffff6a2b061   web::http::client::details::asio_context::handle_write_headers(boost::system::error_code const&) () (/usr/local/lib/libcpprest.so.2.6:??)
#4 0x7ffff6a2261e   boost::asio::detail::write_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, boost::asio::const_buffers_1, boost::asio::detail::transfer_all_t, boost::asio::detail::write_streambuf_handler<std::allocator<char>, boost::_bi::bind_t<void, boost::_mfi::mf1<void, web::http::client::details::asio_context, boost::system::error_code const&>, boost::_bi::list2<boost::_bi::value<std::shared_ptr<web::http::client::details::asio_context> >, boost::arg<1> (*)() (/usr/local/lib/libcpprest.so.2.6:??)
#5 0x7ffff6a227d4   boost::asio::detail::reactive_socket_send_op<boost::asio::const_buffers_1, boost::asio::detail::write_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, boost::asio::const_buffers_1, boost::asio::detail::transfer_all_t, boost::asio::detail::write_streambuf_handler<std::allocator<char>, boost::_bi::bind_t<void, boost::_mfi::mf1<void, web::http::client::details::asio_context, boost::system::error_code const&>, boost::_bi::list2<boost::_bi::value<std::shared_ptr<web::http::client::details::asio_context> >, boost::arg<1> (*)() (/usr/local/lib/libcpprest.so.2.6:??)
#6 0x7ffff6a151b1   crossplat::threadpool::thread_start(void*) () (/usr/local/lib/libcpprest.so.2.6:??)
#7 0x7ffff78bc182   start_thread() (/lib/x86_64-linux-gnu/libpthread.so.0:??)
#8 0x7ffff51c347d   clone() (/lib/x86_64-linux-gnu/libc.so.6:??)
Jul 31, 2015 at 8:29 PM
Hmm. This may be an ABI issue -- gcc 5 broke some ABI compatibility with previous versions. Can you triple check that the boost you're including in the cpprest build steps is in fact the boost you built with gcc 5? You can run cmake in verbose mode to get the actual compiler calls, which should help diagnosing which boost is being pulled in.
Jul 31, 2015 at 9:39 PM
I just did a quick check and it looks that it uses the correct boost (1.58) library, the one I compiled...

The http calls actually work it is ONLY the call back. I did not check the Casablanca code but, do you handle the callback in CB code or you just register it with boost?

I will recompile boost again just to make sure.

Thanks,

G
Jul 31, 2015 at 9:58 PM
Ok I just did a full rebuild of Casablanca after cleaning the build directory and now everything seems to be ok.

Thanks,

G.
Marked as answer by roschuma on 7/31/2015 at 4:18 PM
Aug 1, 2015 at 12:18 AM
Good to know that solved your issue :).