0

I have an app which is creating 2 listener sockets, and receiving data from one and tunneling to the other one. Then the data is tunneled back to origin socket etc. However the app crashes after performing tunneling for around 5s. The call stack is always the same.

void ListenerOne::readPerformed(const asio::error_code& error, size_t bytes_transferred)
{
    //Error checking is present there

    //Write data to Tunnel (tunnelBackSession is pointer to class)
    this->tunnelBackSession->writeDataWithBufferCopy(data_, bytes_transferred);
}

_data -> buffer defined in class as regular char buffer.

void ListenerTwo::writeDataWithBufferCopy(char* szData, size_t len)
{
    memcpy(data_, szData, len);
    socket_.async_write_some(asio::buffer(data_, len), boost::bind(&ListenerTwo::afterWrite, this, asio::placeholders::error));
}

Before using memcpy, i tried to directly use buffer from the first listener, but it also trigged the crash. This is the call stack:

ConsoleApplication1.exe!std::_Ref_count_base::_Incwref() Line 105   C++
ConsoleApplication1.exe!std::_Ptr_base<void>::_Resetw<void>(void * _Other_ptr, std::_Ref_count_base * _Other_rep) Line 428  C++
ConsoleApplication1.exe!std::_Ptr_base<void>::_Resetw<void>(const std::_Ptr_base<void> & _Other) Line 415   C++
ConsoleApplication1.exe!std::weak_ptr<void>::weak_ptr<void><void,void>(const std::shared_ptr<void> & _Other) Line 1034  C++
ConsoleApplication1.exe!asio::detail::win_iocp_socket_service_base::async_send<asio::mutable_buffers_1,boost::_bi::bind_t<void,boost::_mfi::mf1<void,UserSession,std::error_code const &>,boost::_bi::list2<boost::_bi::value<UserSession *>,boost::arg<1> > > >(asio::detail::win_iocp_socket_service_base::base_implementation_type & impl, const asio::mutable_buffers_1 & buffers, int flags, boost::_bi::bind_t<void,boost::_mfi::mf1<void,UserSession,std::error_code const &>,boost::_bi::list2<boost::_bi::value<UserSession *>,boost::arg<1> > > & handler) Line 225   C++
ConsoleApplication1.exe!asio::stream_socket_service<asio::ip::tcp>::async_send<asio::mutable_buffers_1,boost::_bi::bind_t<void,boost::_mfi::mf1<void,UserSession,std::error_code const &>,boost::_bi::list2<boost::_bi::value<UserSession *>,boost::arg<1> > > >(asio::detail::win_iocp_socket_service<asio::ip::tcp>::implementation_type & impl, const asio::mutable_buffers_1 & buffers, int flags, boost::_bi::bind_t<void,boost::_mfi::mf1<void,UserSession,std::error_code const &>,boost::_bi::list2<boost::_bi::value<UserSession *>,boost::arg<1> > > && handler) Line 331 C++
ConsoleApplication1.exe!asio::basic_stream_socket<asio::ip::tcp,asio::stream_socket_service<asio::ip::tcp> >::async_write_some<asio::mutable_buffers_1,boost::_bi::bind_t<void,boost::_mfi::mf1<void,UserSession,std::error_code const &>,boost::_bi::list2<boost::_bi::value<UserSession *>,boost::arg<1> > > >(const asio::mutable_buffers_1 & buffers, boost::_bi::bind_t<void,boost::_mfi::mf1<void,UserSession,std::error_code const &>,boost::_bi::list2<boost::_bi::value<UserSession *>,boost::arg<1> > > && handler) Line 732  C++
ConsoleApplication1.exe!ListenerTwo::writeDataWithBufferCopy(char * szData, unsigned int len) Line 79   C++
ConsoleApplication1.exe!ListenerOne::readPerformed(const std::error_code & error, unsigned int bytes_transferred) Line 100  C++

I read somewhere that it's related to reference count and is buffer related. How can i fix the issue?

3
  • Nobody can solve this issue just from what you've posted here, this seems like a question seeking help with general debugging skills, not a specific reproducible issue, which is why people are voting to close. If I had to take a stab in the dark, I'd blame the fact that you're not using shared_from_this in your callback binds, which if nothing else is maintaining the lifetime of your encapsulating class, is going to cause the object to self destruct and when asio invokes the callbacks, the object is in an undefined state causing your crashes.
    – user562566
    Oct 13, 2015 at 17:13
  • Thanks for a tip. I'll do more research. I've found a VERY similar issue here: stackoverflow.com/questions/23456782/…. The call stack is almost same.
    – Josh
    Oct 13, 2015 at 17:46
  • As sehe has pointed out in the other post, it's a matter of objects going dead on the call return. Either your encapsulating class or a member that you're passing to the callback is dying mid-call
    – user562566
    Oct 13, 2015 at 17:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.