boost::asio + std::future – Access violation after closing socket

recvmsg is receiving into a buffer (streambuf) that was freed after throwing the exception in TCPClient::sendMessage (line 105, end of scope). You forgot to cancel the asynchronous operation (async_read_until) started in line 97. Fix it: else { socket->cancel(); // ADDED std::cout << “socket points to ” << std::addressof(*socket) << std::endl; throw std::runtime_error(“timeout”); } Or even, … Read more

When must you pass io_context to boost::asio::spawn? (C++)

Asio has added the concept of associated executors and default executors. The associated executors is not really new, because the handler_invoke protocol already allowed for handler-type specific semantics. However, since the formulation of the executor concept it became more generalized. Now you can post any handler, and it will execute on the associated executor, the … Read more

Some clarification needed about synchronous versus asynchronous asio operations

The Boost.Asio documentation really does a fantastic job explaining the two concepts. As Ralf mentioned, Chris also has a great blog describing asynchronous concepts. The parking meter example explaining how timeouts work is particularly interesting, as is the bind illustrated example. First, consider a synchronous connect operation: The control flow is fairly straightforward here, your … Read more

Boost linker error: Unresolved external symbol “class boost::system::error_category const & __cdecl boost::system::get_system_category(void)”

I solved the problem. I had built 32-bit libraries when I had intended to build 64-bit libraries. I fixed up my build statement, and built 64-bit libraries, and now it works. Here is my bjam command line: C:\Program Files (x86)\boost\boost_1_38>bjam –build-dir=c:\boost –build-type=complete –toolset=msvc-9.0 address-model=64 architecture=x86 –with-system

C++ Boost ASIO simple periodic timer?

A very simple, but fully functional example: #include <iostream> #include <boost/asio.hpp> boost::asio::io_service io_service; boost::posix_time::seconds interval(1); // 1 second boost::asio::deadline_timer timer(io_service, interval); void tick(const boost::system::error_code& /*e*/) { std::cout << “tick” << std::endl; // Reschedule the timer for 1 second in the future: timer.expires_at(timer.expires_at() + interval); // Posts the timer event timer.async_wait(tick); } int main(void) { // … Read more

What is the proper way to securely disconnect an asio SSL socket?

To securely disconnect, perform a shutdown operation and then close the underlying transport once shutdown has complete. Hence, the method you are currently using will perform a secure disconnect: boost::system::error_code ec; ssl_socket.cancel(ec); ssl_socket.async_shutdown([](…) { ssl_socket.close(); }; Be aware that the current async_shutdown operation will be considered complete when either: A close_notify has been received by … Read more

Official “Boost library” Support for Android and iOS? [closed]

Got reply from boost community Yes. These platforms are not officially supported because no one has volunteered to run regression tests regularly for them. It is not possible for a Boost developer to test on all platforms. So developers depend on the test results of regression tests run by volunteers. For example, see http://beta.boost.org/development/tests/trunk/developer/summary.html If … Read more

c++ work queues with blocking

Well. That’s really quite simple; You’re rejecting the tasks posted! template< typename Task > void run_task(task task){ boost::unique_lock<boost::mutex> lock( mutex_ ); if(0 < available_) { –available_; io_service_.post(boost::bind(&tpool::wrap_task, this, boost::function< void() > ( task ))); } } Note that the lock “waits” until the mutex is not owned by a thread. This might already be the … Read more

Boost::asio – how to interrupt a blocked tcp server thread?

In short, there are two options: Change code to be asynchronous (acceptor::async_accept() and async_read), run within the event loop via io_service::run(), and cancel via io_service::stop(). Force blocking calls to interrupt with lower level mechanics, such as signals. I would recommend the first option, as it is more likely to be the portable and easier to … Read more