下面是一个在运行时引发访问冲突的小片段。我正在使用 Visual Studio 2015 和 boost 1.60.0 以及调用时
m_socket.async_connect(end_point, yieldw[ec]);
升压文件“socket_ops.ipp”中发生访问冲突
"socket_type s = error_wrapper(::WSASocketW(af, type, protocol, 0, 0,WSA_FLAG_OVERLAPPED),ec);"
异常是“ConsoleApplication4.exe 中 0x000007FEFD598A2F (KernelBase.dll) 处的未处理异常:0xC0000005:访问冲突读取位置 0xFFFFFFFFFFFFFFFF。”
#include "stdafx.h"
#include <memory>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/write.hpp>
#include <cassert>
#include <thread>
#include <chrono>
int main()
{
boost::asio::io_service m_io_service;
//boost::asio::io_service::strand m_socket_strand{ m_io_service };
boost::asio::io_service::work m_work(m_io_service);
boost::asio::ip::tcp::socket m_socket(m_io_service);
std::thread thread([&]() {
m_io_service.run();
});
boost::asio::ip::tcp protocol_family{ boost::asio::ip::tcp::v4() };
boost::asio::ip::tcp::resolver::query query(protocol_family, "localhost", std::to_string(2101));
boost::asio::ip::tcp::resolver resolver(m_io_service);
boost::asio::ip::tcp::resolver::iterator iterator{ resolver.resolve(query) }; // Hosts can have multiple addresses.
/*boost::asio::ip::tcp::resolver::iterator end_iterator{ iterator };
end_iterator++;
boost::asio::ip::tcp::resolver::iterator end;
if (end_iterator != end)
{
std::ostringstream endpoint_descripiton;
}*/
assert(iterator != boost::asio::ip::tcp::resolver::iterator());
auto end_point = iterator->endpoint();
boost::asio::spawn(m_io_service, [&, end_point](boost::asio::yield_context yieldw)
{
boost::system::error_code ec;
m_socket.async_connect(end_point, yieldw[ec]);
if (ec != boost::system::errc::success)
{
std::this_thread::sleep_for(std::chrono::seconds(5));
}
else
{
}
});
std::this_thread::sleep_for(std::chrono::seconds(20));
return 0;
}