1

I'm writing a smpp client and I have a problem that I can't solve. I wrote a tcp_client class with an internal io_service class to encapsulate all the process of the tcp comunication. After that I realize that I need that control in inherited classes, to run another tasks to be done, so, I modified the tcp_client to initialize the io_service in the constructor of the inherited class, and post from there.

The tcp_client class:

class tcp_client
{
public:
    tcp_client(boost::asio::io_service &_io_service, tcp::resolver::iterator endpoint_iterator)
      : m_io_service(_io_service), socket_(_io_service)
    {
        connect_start(endpoint_iterator);
        // this works OK
    }

    tcp_client(boost::asio::io_service &_io_service)
    :m_io_service(_io_service),socket_(m_io_service)
    {
        // SIGSEGV
    }

    tcp_client(void)
    :m_io_service(tcp_io_service),socket_(m_io_service)
    {
        // Works OK
    }

    void connect(const char *host, const char *port)
    {
    if( get_tcp_status()==CONNECTED || get_tcp_status()==CONNECTING )
    {
        do_close();
    }
    tcp::resolver::query query(host,port);
    tcp::resolver resolver(m_io_service);
    tcp::resolver::iterator iterator = resolver.resolve(query);
    connect_start(iterator);
    m_io_service_thread = boost::thread(boost::bind(&boost::asio::io_service::run,&m_io_service));
    }
private:
    boost::asio::io_service &tcp_io_service;
    .........................
    .........................
}

In the constructor

tcp_client(boost::asio::io_service &_io_service)
        :m_io_service(_io_service),socket_(m_io_service)
        {

        }

I receive a SIGSEGV signal, about a mutex. The others contructors works very well, but this stop me. I'm able to continue the develop using the other constructors that works OK, but I want to know what is the problem here, just for sport (to learn).

The inherited class that I use for this is:

class smpp_client : public tcp_client
{
private:
   boost::asio::io_service smpp_io_service;
public:
    smpp_client(boost::asio::io_service &_io_service, tcp::resolver::iterator endpoint_iterator)
      :tcp_client(_io_service,endpoint_iterator),
       m_smpp_status(SC_BIND_DISCONNECTED),
       rx_thread_exit(false)
      {
     m_windowing = 10;
     initialize_stack();
      }

   smpp_client(void):
      tcp_client(smpp_io_service),
      m_smpp_status(SC_BIND_DISCONNECTED),
      rx_thread_exit(false)
      {
     m_windowing = 10;
     initialize_stack();
     //smpp_io_service.post(boost::bind(&smpp_client::rx_loop,this));
      }
...........
...........
}

The first constructor in this class works OK, but the second call the constructor in tcp_client that rise a SIGSERV signal.

The main is:

/*boost::asio::io_service io_service;
tcp::resolver::query query(argv[1],argv[2]);
tcp::resolver resolver(io_service);
tcp::resolver::iterator iterator = resolver.resolve(query);

smpp_client c(io_service,iterator);

boost::thread t(boost::bind(&boost::asio::io_service::run,&io_service));*/

smpp_client c; // --> HERE SIGSEGV

I have tested with gdb and valgrind, the information in the output is the same:

Program received signal SIGSEGV, Segmentation fault. 0x00007ffff6bdbe64 in pthread_mutex_lock () from /lib64/libpthread.so.0

I check all addresses and values at that point, and I have no idea how to fix it.

I'm using Linux Slackware 13.37 with boost 1.49 and gcc 4.7.1

PD: complete code can be watched at https://github.com/jpcordovae/smpp_client but not updated at this error.

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
JP Cordova
  • 119
  • 9

2 Answers2

2

The second constructor does not work because your passing reference to not-initialized member smpp_io_service as a parameter of base class constructor. In c++ first all base classes constructors are called and than non-static members are initiated.

See this question for more details about initialization order.

When using first constructor the io_service is created earlier that is why the code works just fine.

Community
  • 1
  • 1
Maciek B
  • 394
  • 1
  • 7
  • TY very much, I make the smpp_io_service static and it works perfect again :) – JP Cordova Feb 25 '13 at 18:43
  • Anyways, I checked if before with GDB and I have no problems reading a valid address of the smpp_io_service, so, correct me, if I think that a valid address doesn't means a valid initialization. – JP Cordova Feb 25 '13 at 18:59
  • yes, you are correct. The address is valid because the memory is already allocated - only the constructor is not yet executed – Maciek B Feb 25 '13 at 19:32
0

Try taking a look at the _io_service argument. Make sure it has been defined properly and is not zero. Also, the socket_ object is your socket object, which you appear to be initializing to m_io_service. The socket object is not an io_service object. Make sure that this is being initialized to the proper type of socket object. I'm using SSL, so this is how I initialize it:

boost::shared_ptr<boost::asio::io_service> IOServ(new boost::asio::io_service);
IOService = IOServ;
pSocket = new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(*IOService, ctx);
boost::asio::async_connect(pSocket->lowest_layer(), EndpointIterator,
   boost::bind(&SSLSocket::HandleConnect, this, boost::asio::placeholders::error));
Bob Bryan
  • 3,687
  • 1
  • 32
  • 45
  • I did it and GDB show me a valid address, or I think it was a valid addres, maybe is time to search for a good book over c++ with a initialization chapter. – JP Cordova Feb 25 '13 at 19:01