Search code examples
c++boostboost-asioshared-ptr

Boost shared_ptr object having problems with getting values


Ok... I've spent too much time one this.

What I am doing is making a list of connections, "strangers" is just a unconfirmed, or denied connection that waits for an ack to make sure that the client got the message. The host has a vector of these strangers as boost::shared_ptr, and when a stranger that is accepted, and acked, it becomes a client, but thats not the problem.

When a connection is received, a new stranger is made, but I get a runtime error when calling for the endpoint "(*it)->getEndpoint()" inside of sending the packet, but that's just an example, its the same for other values, like "timed_out".

Down here is the stranger class, and the usage of sending

class stranger : public boost::enable_shared_from_this<stranger> //a short lived object that sends the reply if their connection accepted / denied until an ack is received or timed out.
{
private:
  udp::endpoint end_p_;
  std::string data_;
  std::string name_;

  deadline_timer Timeout;
public:

  bool accepted = false; //public to check if this stranger is accepted or not.

  bool timed_out = false;

  //we assume the packet we are sending is accepting the connection because we have the name parameter.
  stranger(udp::endpoint end_p, std::string name, boost::asio::io_service &io_s)
    :Timeout(io_s)
    ,end_p_(end_p)
  {
    std::stringstream ss; //formatting the packet
    ss<<(char)(con_rep_accepted)<<0<<0; //packettype, 2 bytes for pak num(its 0 because its the connection)
    data_ = ss.str();
    name = name_;
    accepted = true;
    timed_out = false;

    Timeout.expires_from_now(boost::posix_time::seconds(5));
    Timeout.async_wait(boost::bind(&stranger::doTimeout, this));
  }
  //we assume the packet we are sending is denying the connection because there is no name.
  stranger(udp::endpoint end_p , boost::asio::io_service& io_s)
    :Timeout(io_s)
  {
    end_p_ = end_p;
    std::stringstream ss; //formatting the packet
    ss<<(char)(con_rep_denied)<<0<<0; //1 byte for packet type, 2 bytes for pak num(its 0 because its the connection)
    data_ = ss.str();
    accepted = false;
    timed_out = false;

    Timeout.expires_from_now(boost::posix_time::seconds(5));
    Timeout.async_wait(boost::bind(&stranger::doTimeout, this));
  }

  void doTimeout()
  {
    std::cout<<"timed.\n";
    timed_out = true;
  }


  udp::endpoint getEndpoint(){return end_p_;} //to compare endpoints

  std::string getData(){return data_;}

  std::string getName(){return name_;}

  ~stranger(){Timeout.cancel();}
};

Where the debugger points to when I get the runtime. A client is essentially exactly the same as a stranger right now, no game data to send. They are also a vector of shared_ptrs.

//Everything that the server receives
void Host_State::Receive()
{
socket_.async_receive_from(
  boost::asio::buffer(in_data,1024), sender_endpoint_,
  [this](boost::system::error_code ec, std::size_t bytes)
  {
    if (!ec && bytes >= 3 ) //the header is 3 bytes, and the smallest usable packet is 3 bytes.
    {

      int pak_type = (int)(in_data[0]);

      unsigned short packet_num = (in_data[1] << 8) | in_data[2]; //the number in which order it is from

      for(auto it = clients.begin(); it!=clients.end(); it++)
        //HERE!------------------------
        if((*it)->getEndpoint() == sender_endpoint_) //<-debugger points here
        //------------------------
        {

Using windows, gcc 4.8.1, boost 1.57.0. Debug Dump:

#0 00435E5C boost::shared_ptr<client>::operator->(this=0x6e6f6974) (C:/boost_1_57_0/boost/smart_ptr/shared_ptr.hpp:648)
#1 00406394 Host_State::__lambda1::operator() (__closure=0x28fc00, ec=..., bytes=67) (C:\Users\poteto\Desktop\pixely_trenches\Sources\Server.cpp:26)
#2 00408EB1 boost::asio::detail::binder2<Host_State::Receive()::__lambda1, boost::system::error_code, unsigned int>::operator()(void)(this=0x28fc00) (C:/boost_1_57_0/boost/asio/detail/bind_handler.hpp:127)
#3 00408E61 boost::asio::asio_handler_invoke<boost::asio::detail::binder2<Host_State::Receive()::__lambda1, boost::system::error_code, unsigned int> >(boost::asio::detail::binder2<Host_State::Receive()::__lambda1, boost::system::error_code, unsigned int> &, ...)(function=...) (C:/boost_1_57_0/boost/asio/handler_invoke_hook.hpp:69)
#4 00408D55 boost_asio_handler_invoke_helpers::invoke<boost::asio::detail::binder2<Host_State::Receive()::__lambda1, boost::system::error_code, unsigned int>, Host_State::Receive()::__lambda1>(boost::asio::detail::binder2<Host_State::Receive()::__lambda1, boost::system::error_code, unsigned int> &, Host_State::__lambda1 &)(function=..., context=...) (C:/boost_1_57_0/boost/asio/detail/handler_invoke_helpers.hpp:37)
#5 00408993 boost::asio::detail::win_iocp_socket_recvfrom_op<boost::asio::mutable_buffers_1, boost::asio::ip::basic_endpoint<boost::asio::ip::udp>, Host_State::Receive()::__lambda1>::do_complete(boost::asio::detail::io_service_impl *, boost::asio::detail::operation *, const boost::system::error_code &, std::size_t)(owner=0x9cff48, base=0x9c3510, result_ec=..., bytes_transferred=67) (C:/boost_1_57_0/boost/asio/detail/win_iocp_socket_recvfrom_op.hpp:104)
#6 00423CD0 boost::asio::detail::win_iocp_operation::complete(this=0x9c3510, owner=..., ec=..., bytes_transferred=67) (C:/boost_1_57_0/boost/asio/detail/win_iocp_operation.hpp:46)
#7 00424DF5 boost::asio::detail::win_iocp_io_service::do_one(this=0x9cff48, block=true, ec=...) (C:/boost_1_57_0/boost/asio/detail/impl/win_iocp_io_service.ipp:405)
#8 00424AA1 boost::asio::detail::win_iocp_io_service::run(this=0x9cff48, ec=...) (C:/boost_1_57_0/boost/asio/detail/impl/win_iocp_io_service.ipp:164)
#9 0041E65A boost::asio::io_service::run(this=0x511391c) (C:/boost_1_57_0/boost/asio/impl/io_service.ipp:59)
#10 0041645E    _fu15___ZSt4cout() (C:/Users/poteto/Desktop/pixely_trenches/Sources/Host.h:232)
#11 00405A87    ChangeState() (C:\Users\poteto\Desktop\pixely_trenches\Sources\main.cpp:44)
#12 00405C90    SDL_main(argc=argc@entry=1, argv=argv@entry=0x3b0008) (C:\Users\poteto\Desktop\pixely_trenches\Sources\main.cpp:98)
#13 0040C11C    console_main(argc=argc@entry=1, argv=argv@entry=0x3b0008) (../src/main/windows/SDL_windows_main.c:140)
#14 0040C2DD    WinMain@16(hInst=0x400000, hPrev=0x0, szCmdLine=0x9d3ab4 "", sw=10) (../src/main/windows/SDL_windows_main.c:177)
#15 00448C1B    main () (??:??)

Solution

  • It was a code accident, there was iterator invalidation:

    for(auto it= clients.begin(); it!=clients.end(); )
    {
      if((*it).timed_out)
      {
        clients.erase(it);
      }
      
      ++it; //I should've put this into an else.
    }
    

    The problem was also because I had my "in_data[]" initialized just like that without the number.