c++macosboosthomebrewcpp-netlib

cpp-Netlib with Boost Mac OSX seg fault when using HTTP Client body


I recently started learning cpp-netlib which requires Boost and am running into some issues when trying to compile one of the cpp-netlib examples from there site http://cpp-netlib.org/0.11.2/examples/http/http_client.html. I have never used Boost before and am wondering how I should go about figuring out what to link when compiling my program? I am using homebrew as my package manager currently. I installed boost by running

brew install boost --c++11

I then installed cpp-netlib by running

brew install cpp-netlib

I copied and pasted the code example from the site and hardcoded the request URI

#include <boost/network/protocol/http/client.hpp>
#include <iostream>
int main()
{
    using namespace boost::network;
    http::client client;
    http::client::request request("http://www.boost.org");
    request << header("Connection", "close");
    http::client::response response = client.get(request);
    std::cout << body(response) << std::endl;
    return 0;
}

After hours of Google searches with my roommate we found what boost libraries I needed to link, we also found that clang++ was finding my Macs old outdated openssl that is preinstalled in to the operating system and not the new version that I had installed through homebrew. With this new knowledge we found that compiling with this command works

clang++ -g -I /usr/local/Cellar/openssl/1.0.2e/include test.cpp -L /usr/local/Cellar/openssl/1.0.2e/lib -lboost_system-mt -lboost_thread-mt -lcppnetlib-client-connections -lcppnetlib-uri -lcppnetlib-server-parsers -lssl -lcrypto

I am now getting a segmentation fault when running the program. When I attempt to run lldb to debug my program lldb exits with a "bus error: 10". I found that the seg fault is occurring when running this line of code

std::cout << body(response) << std::endl;

When I run valgrind I get the following output

==1123== Memcheck, a memory error detector
==1123== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==1123== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==1123== Command: ./a.out
==1123== 
--1123-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--1123-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--1123-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
--1123-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 8 times)
==1123== Thread 2:
==1123== Jump to the invalid address stated on the next line
==1123==    at 0x0: ???
==1123==    by 0x100016A5A: boost::asio::detail::task_io_service::do_run_one(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&, boost::asio::detail::task_io_service_thread_info&, boost::system::error_code const&) (task_io_service.ipp:372)
==1123==    by 0x10001669A: boost::asio::detail::task_io_service::run(boost::system::error_code&) (task_io_service.ipp:149)
==1123==    by 0x10000FDD0: boost::asio::io_service::run() (io_service.ipp:59)
==1123==    by 0x1000196D1: boost::_mfi::mf0<unsigned long, boost::asio::io_service>::operator()(boost::asio::io_service*) const (in ./a.out)
==1123==    by 0x100019620: unsigned long boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> >::operator()<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list0>(boost::_bi::type<unsigned long>, boost::_mfi::mf0<unsigned long, boost::asio::io_service>&, boost::_bi::list0&, long) (in ./a.out)
==1123==    by 0x10001959B: boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > >::operator()() (in ./a.out)
==1123==    by 0x10001874B: boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >::run() (thread.hpp:116)
==1123==    by 0x1003ADFD5: boost::(anonymous namespace)::thread_proxy(void*) (in /usr/local/Cellar/boost/1.59.0/lib/libboost_thread-mt.dylib)
==1123==    by 0x100B289B0: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==1123==    by 0x100B2892D: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==1123==    by 0x100B26384: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==1123==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==1123== 
==1123== 
==1123== Process terminating with default action of signal 11 (SIGSEGV)
==1123==  Bad permissions for mapped region at address 0x0
==1123==    at 0x0: ???
==1123==    by 0x100016A5A: boost::asio::detail::task_io_service::do_run_one(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&, boost::asio::detail::task_io_service_thread_info&, boost::system::error_code const&) (task_io_service.ipp:372)
==1123==    by 0x10001669A: boost::asio::detail::task_io_service::run(boost::system::error_code&) (task_io_service.ipp:149)
==1123==    by 0x10000FDD0: boost::asio::io_service::run() (io_service.ipp:59)
==1123==    by 0x1000196D1: boost::_mfi::mf0<unsigned long, boost::asio::io_service>::operator()(boost::asio::io_service*) const (in ./a.out)
==1123==    by 0x100019620: unsigned long boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> >::operator()<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list0>(boost::_bi::type<unsigned long>, boost::_mfi::mf0<unsigned long, boost::asio::io_service>&, boost::_bi::list0&, long) (in ./a.out)
==1123==    by 0x10001959B: boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > >::operator()() (in ./a.out)
==1123==    by 0x10001874B: boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >::run() (thread.hpp:116)
==1123==    by 0x1003ADFD5: boost::(anonymous namespace)::thread_proxy(void*) (in /usr/local/Cellar/boost/1.59.0/lib/libboost_thread-mt.dylib)
==1123==    by 0x100B289B0: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==1123==    by 0x100B2892D: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==1123==    by 0x100B26384: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==1123== 
==1123== HEAP SUMMARY:
==1123==     in use at exit: 150,091 bytes in 3,656 blocks
==1123==   total heap usage: 3,912 allocs, 256 frees, 194,518 bytes allocated
==1123== 
==1123== LEAK SUMMARY:
==1123==    definitely lost: 400 bytes in 4 blocks
==1123==    indirectly lost: 80,493 bytes in 2,673 blocks
==1123==      possibly lost: 0 bytes in 0 blocks
==1123==    still reachable: 47,160 bytes in 790 blocks
==1123==         suppressed: 22,038 bytes in 189 blocks
==1123== Rerun with --leak-check=full to see details of leaked memory
==1123== 
==1123== For counts of detected and suppressed errors, rerun with: -v
==1123== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Killed: 9

Any help is much appreciated!


Solution

  • I found that to get rid of the segmentation fault I needed to include -std=c++11 making the command to compile

    clang++ -std=c++11 -I /usr/local/Cellar/openssl/1.0.2e/include test.cpp -L /usr/local/Cellar/openssl/1.0.2e/lib -lboost_system -lboost_thread-mt -pthread -lcppnetlib-client-connections -lcppnetlib-uri -lcppnetlib-server-parsers -lssl -lcrypto
    

    I should probably create a make file.