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.