我一直在嘗試運行幾個boost :: asio示例。他們編譯和鏈接沒有問題,但是當他們嘗試在io_service上註冊async_accept時,他們正在拋出分段錯誤。我附上了下面的代碼。它沒有從boost文檔修改過。在Visual Studio 2013 boost :: asio示例代碼段錯誤
//
// server.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <ctime>
#include <iostream>
#include <string>
#include <memory>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
class session {
public:
session(boost::asio::io_service& io_service)
: socket_(io_service) {}
tcp::socket& socket() {
return socket_;
}
void start() {
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_read(const boost::system::error_code& error,
size_t bytes_transferred) {
if (!error) {
boost::asio::async_write(socket_,
boost::asio::buffer(data_, bytes_transferred),
boost::bind(&session::handle_write, this,
boost::asio::placeholders::error));
}
else {
delete this;
}
}
void handle_write(const boost::system::error_code& error) {
if (!error) {
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else {
delete this;
}
}
private:
tcp::socket socket_;
enum { max_length = 1024 };
char data_[max_length];
};
class server {
public:
server(boost::asio::io_service& io_service, short port)
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), port)) {
session* new_session = new session(io_service_);
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
}
void handle_accept(session* new_session,
const boost::system::error_code& error) {
if (!error) {
new_session->start();
new_session = new session(io_service_);
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
}
else {
delete new_session;
}
}
private:
boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
};
回溯:
> gryphon.exe!boost::asio::detail::win_iocp_io_service::work_started() Line 96 C++ gryphon.exe!boost::asio::detail::win_iocp_socket_service_base::start_accept_op(boost::asio::detail::win_iocp_socket_service_base::base_implementation_type & impl, bool peer_is_open, boost::asio::detail::socket_holder & new_socket, int family, int type, int protocol, void * output_buffer, unsigned long address_length, boost::asio::detail::win_iocp_operation * op) Line 480 C++
gryphon.exe!boost::asio::detail::win_iocp_socket_service<boost::asio::ip::tcp>::async_accept<boost::asio::basic_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::_bi::bind_t<void,boost::_mfi::mf2<void,server,session *,boost::system::error_code const &>,boost::_bi::list3<boost::_bi::value<server *>,boost::_bi::value<session *>,boost::arg<1> > > >(boost::asio::detail::win_iocp_socket_service<boost::asio::ip::tcp>::implementation_type & impl, boost::asio::basic_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > & peer, boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> * peer_endpoint, boost::_bi::bind_t<void,boost::_mfi::mf2<void,server,session *,boost::system::error_code const &>,boost::_bi::list3<boost::_bi::value<server *>,boost::_bi::value<session *>,boost::arg<1> > > & handler) Line 487 C++
gryphon.exe!boost::asio::socket_acceptor_service<boost::asio::ip::tcp>::async_accept<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp>,boost::_bi::bind_t<void,boost::_mfi::mf2<void,server,session *,boost::system::error_code const &>,boost::_bi::list3<boost::_bi::value<server *>,boost::_bi::value<session *>,boost::arg<1> > > >(boost::asio::detail::win_iocp_socket_service<boost::asio::ip::tcp>::implementation_type & impl, boost::asio::basic_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > & peer, boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> * peer_endpoint, boost::_bi::bind_t<void,boost::_mfi::mf2<void,server,session *,boost::system::error_code const &>,boost::_bi::list3<boost::_bi::value<server *>,boost::_bi::value<session *>,boost::arg<1> > > && handler, void * __formal) Line 285 C++
gryphon.exe!boost::asio::basic_socket_acceptor<boost::asio::ip::tcp,boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >::async_accept<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp>,boost::_bi::bind_t<void,boost::_mfi::mf2<void,server,session *,boost::system::error_code const &>,boost::_bi::list3<boost::_bi::value<server *>,boost::_bi::value<session *>,boost::arg<1> > > >(boost::asio::basic_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > & peer, boost::_bi::bind_t<void,boost::_mfi::mf2<void,server,session *,boost::system::error_code const &>,boost::_bi::list3<boost::_bi::value<server *>,boost::_bi::value<session *>,boost::arg<1> > > && handler, void * __formal) Line 1020 C++
gryphon.exe!server::server(boost::asio::io_service & io_service, short port) Line 75 C++
gryphon.exe!main() Line 32 C++
[External Code]
我以前得到這個代碼工作,但此後重新編譯64位提升(地址模型-64)。我認爲這是錯誤的原因,但我不明白Visual Studio或Boost的構建設置足夠了解。
編輯: 編譯器選項
/GS /analyze- /W3 /Zc:wchar_t /I"src" /I"include" /I"C:\boost\boost_1_55_0" /ZI /Gm /Od /Fd"Debug\vc120.pdb" /fp:precise /D "_CRT_SECURE_NO_DEPRECATE" /D "_WIN32_WINDOWS" /D "WPCAP" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\gryphon.pch"
連接器選項
/OUT:"C:\Users\USER\Documents\Visual Studio 2013\Projects\gryphon\Debug\gryphon.exe" /MANIFEST /NXCOMPAT /PDB:"C:\Users\USER\Documents\Visual Studio 2013\Projects\gryphon\Debug\gryphon.pdb" /DYNAMICBASE "Ws2_32.lib" "Packet.lib" "wpcap.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /MACHINE:X86 /INCREMENTAL /PGD:"C:\Users\USER\Documents\Visual Studio 2013\Projects\gryphon\Debug\gryphon.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"Debug\gryphon.exe.intermediate.manifest" /ERRORREPORT:PROMPT /NOLOGO /LIBPATH:"lib" /LIBPATH:"C:\boost\boost_1_55_0\stage\lib" /TLBID:1
升壓/級/ lib目錄包含32位預編譯庫。希望瞭解有關32位/ 64位/多線程/靜態boost庫與Windows運行時庫之間差異的任何見解。
快速瀏覽它看起來很好。嘗試一個完整的重建。重新檢查所有項目/庫共享編譯器/鏈接器選項 – sehe 2015-04-05 18:03:11
重建升級沒有任何其他選項。如果這不起作用,我會用這段代碼開始一個新的VS項目,看看是否有效。謝謝 – user2411693 2015-04-05 18:13:18