Я пытаюсь выполнить файл bingrequest.cpp
, предоставленный библиотекой cpprestsdk
.
Вот исходный код файла bingrequest.cpp
( cpprestsdk github getstarted )
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
int main(int argc, char* argv[])
{
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("http://www.bing.com/"));
// Build request URI and start the request.
uri_builder builder(U("/search"));
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());
}
return 0;
}
Файл CMakeLists.txt
:
add_executable(BingRequest bingrequest.cpp)
set(REST_LIBRARIES "-lboost_system -lcrypto -lssl -lboost_log -lboost_thread -lcpprest")
set(CMAKE_CXX_FLAGS "-std=c++14 -Wall -g")
target_link_libraries(BingRequest ${REST_LIBRARIES})
Исходный файл успешно компилируется в моей системе.
Но он вызывает ошибку сегментации.Я использовал gdb и обнаружил, что программа успешно работала до requestTask.wait();
и вылетала после выполнения вышеуказанной строки.
Вот обратная трассировка от gdb:
(gdb) bt
#0 __GI___libc_free (mem=0x85) at malloc.c:3103
#1 0x00005555556a582a in web::http::details::_http_response::~_http_response (this=0x7fffcc002700, __in_chrg=<optimized out>)
at /usr/local/include/cpprest/http_msg.h:474
#2 0x00005555556a5894 in __gnu_cxx::new_allocator<web::http::details::_http_response>::destroy<web::http::details::_http_response> (this=0x7fffcc002700,
__p=0x7fffcc002700) at /usr/include/c++/7/ext/new_allocator.h:140
#3 0x00005555556a1c49 in std::allocator_traits<std::allocator<web::http::details::_http_response> >::destroy<web::http::details::_http_response> (__a=...,
__p=0x7fffcc002700) at /usr/include/c++/7/bits/alloc_traits.h:487
#4 0x000055555569bf9b in std::_Sp_counted_ptr_inplace<web::http::details::_http_response, std::allocator<web::http::details::_http_response>, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x7fffcc0026f0) at /usr/include/c++/7/bits/shared_ptr_base.h:535
#5 0x00007ffff7373b46 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() () from /usr/lib/x86_64-linux-gnu/libcpprest.so.2.10
#6 0x00007ffff7373cb6 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::operator=(std::__shared_count<(__gnu_cxx::_Lock_policy)2> const&) ()
from /usr/lib/x86_64-linux-gnu/libcpprest.so.2.10
#7 0x00007ffff736fec7 in web::http::client::details::request_context::complete_headers() () from /usr/lib/x86_64-linux-gnu/libcpprest.so.2.10
#8 0x00007ffff748f3ea in web::http::client::details::asio_context::read_headers() () from /usr/lib/x86_64-linux-gnu/libcpprest.so.2.10
#9 0x00007ffff74904ca in web::http::client::details::asio_context::handle_status_line(boost::system::error_code const&) ()
from /usr/lib/x86_64-linux-gnu/libcpprest.so.2.10
#10 0x00007ffff7478238 in boost::asio::detail::read_until_delim_string_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, 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> (*)()> > >::operator()(boost::system::error_code const&, unsigned long, int) () from /usr/lib/x86_64-linux-gnu/libcpprest.so.2.10
#11 0x00007ffff7478a28 in boost::asio::detail::reactive_socket_recv_op<boost::asio::mutable_buffers_1, boost::asio::detail::read_until_delim_string_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, 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> (*)()> > > >::do_complete(boost::asio::detail::task_io_service*, boost::asio::detail::task_io_service_operation*, boost::system::error_code const&, unsigned long) () from /usr/lib/x86_64-linux-gnu/libcpprest.so.2.10
#12 0x00007ffff7468740 in ?? () from /usr/lib/x86_64-linux-gnu/libcpprest.so.2.10
#13 0x00007ffff73f06f5 in boost_asio_detail_posix_thread_function () from /usr/lib/x86_64-linux-gnu/libcpprest.so.2.10
#14 0x00007ffff66ae6db in start_thread (arg=0x7ffff1e92700) at pthread_create.c:463
#15 0x00007ffff69e788f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
Вывод gdb мне не понятен (относительно исходного файла bingrequest.cpp
).Любые идеи, почему эта ошибка сегментации происходит?
Спасибо!