Reputation: 641
I am writing a client server demo using boost.asio. I went through the http demo and wrote my own code from scratch. But, I am getting an exception (most probably) while acceptor is getting initialized. I ma not sure why I am getting this exception and my example http code (from boost site) is doing same thing and running fine.
Here is my server.h code sample,
#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>
#include "Connection.h"
#include <iostream>
using boost::asio::ip::tcp;
class Server : private boost::noncopyable
{
public:
explicit Server(const std::string& address, const std::string& port);
void run();
private:
boost::asio::ip::tcp::acceptor _acceptor;
boost::asio::io_service _ioService;
CONNECTION_SHARED_POINTER _connection;
void start_accept();
void handle_accept(const boost::system::error_code& e);
void handle_stop();
};
server.cpp code
#include "Server.h"
#include <boost/bind.hpp>
Server::Server(const std::string& address, const std::string& port)
:_acceptor(_ioService),
_connection()
{
boost::asio::ip::tcp::resolver resolver(_ioService);
boost::asio::ip::tcp::resolver::query query(address, port);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
_acceptor.open(endpoint.protocol());
_acceptor.set_option(
boost::asio::ip::tcp::acceptor::reuse_address(true));
_acceptor.bind(endpoint);
_acceptor.listen();
//Starting the acceptor
start_accept();
}
void Server::run()
{
std::cout<< "Running ioService" <<std::endl;
_ioService.run();
}
void Server::start_accept()
{
_connection.reset(new Connection(_ioService));
std::cout<< "Accept connection" <<std::endl;
_acceptor.async_accept( _connection->socket(),
boost::bind( &Server::handle_accept, this,
boost::asio::placeholders::error));
}
void Server::handle_accept(const boost::system::error_code& e)
{
if (!e)
{
_connection->start();
}
start_accept();
}
And my main.cpp
#include "Server.h"
#include <iostream>
int main(int argc, char* argv[])
{
try
{
Server s("0.0.0.0", "7000");
s.run();
}
catch (std::exception& e)
{
std::cerr << "exception: " << e.what() << "\n";
}
return 0;
}
Now my code is getting an excpetion inside Server CTOR, while tryig to init _acceptor. The error message is as follows:
Unhandled exception at 0x7747d7f6 in VrfServer.exe: 0xC0000005: Access violation reading location 0xffffffffffffffff.
Stack dump is as follows:
ntdll.dll!000000007747d7f6()
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
VrfServer.exe!boost::asio::detail::win_mutex::lock() Line 51 C++
VrfServer.exe!boost::asio::detail::scoped_lock<boost::asio::detail::win_mutex>::scoped_lock<boost::asio::detail::win_mutex>(boost::asio::detail::win_mutex & m) Line 47 C++
VrfServer.exe!boost::asio::detail::service_registry::do_use_service(const boost::asio::io_service::service::key & key, boost::asio::io_service::service * (boost::asio::io_service &)* factory) Line 108 + 0x12 bytes C++
VrfServer.exe!boost::asio::detail::service_registry::use_service<boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >() Line 49 C++
VrfServer.exe!boost::asio::use_service<boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >(boost::asio::io_service & ios) Line 34 C++
VrfServer.exe!boost::asio::basic_io_object<boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >::basic_io_object<boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >(boost::asio::io_service & io_service) Line 91 + 0x36 bytes C++
VrfServer.exe!boost::asio::basic_socket_acceptor<boost::asio::ip::tcp,boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >::basic_socket_acceptor<boost::asio::ip::tcp,boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >(boost::asio::io_service & io_service) Line 86 C++
VrfServer.exe!Server::Server(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & address, const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & port) Line 8 + 0x76 bytes C++
VrfServer.exe!main(int argc, char * * argv) Line 8 + 0x52 bytes C++
VrfServer.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C
VrfServer.exe!mainCRTStartup() Line 371 C
kernel32.dll!00000000772259bd()
ntdll.dll!000000007745a2e1()
Can anyone shed any light?
Upvotes: 0
Views: 911
Reputation: 4367
The problem has nothing to do with asio, it's just how C++
works
In your Server
class you declare:
private:
boost::asio::ip::tcp::acceptor _acceptor;
boost::asio::io_service _ioService;
CONNECTION_SHARED_POINTER _connection;
And then you initialize it as follows:
Server::Server(const std::string& address, const std::string& port)
:_acceptor(_ioService),
_connection()
When you initialize _acceptor
, _ioService
has not yet been initialized, due to how the order of declaration in the class.
boost::asio::io_service _ioService;
boost::asio::ip::tcp::acceptor _acceptor;
CONNECTION_SHARED_POINTER _connection;
Should solve the problem.
Upvotes: 1