Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

boost::asio::ip::tcp::resolver::iterator - null/invalid?

13 views
Skip to first unread message

Christopher Pisz

unread,
Mar 22, 2017, 10:37:06 AM3/22/17
to
I've got a block of code that throws an Access Violation when executed. It is the async_connect handler for boost::asio.

//------------------------------------------------------------------------------
void MyClient::OnConnect(const boost::system::error_code & errorCode, boost::asio::ip::tcp::resolver::iterator endpoint)
{
if (errorCode || endpoint == boost::asio::ip::tcp::resolver::iterator())
{
// Error - An error occured while attempting to connect
// Most likely these error codes correspond to https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
std::ostringstream msg;
msg << "An error occured while attempting to connect to " << endpoint->host_name()
<< ". Error code: " << errorCode
<< ". Error Message: " << ErrorCodeToString(errorCode);

LogError(msg.str().c_str(), __FUNCSIG__, __FILE__, __LINE__);
return;
}

// We connected to an endpoint
m_connectionState |= CONNECTED;

In the debugger it looks like the problem is inside endpoint->host_name(), because it tried to get values_[0] while values_ are null.

This is a common Connection Refused scenario. I thought the handler got the endpoint so that it knew who it was trying to connect to! Is there some manner of check I can do on the iterator before I try to call a method on it?

It seems to pass and still throw access violation on
if( endpoint != boost::asio::ip::tcp::resolver::iterator() )
{
std::string blah = endpoint->host_name();
}

Jorgen Grahn

unread,
Mar 22, 2017, 2:46:55 PM3/22/17
to
On Wed, 2017-03-22, Christopher Pisz wrote:
> I've got a block of code that throws an Access Violation when
> executed. It is the async_connect handler for boost::asio.
>
> //------------------------------------------------------------------------------
> void MyClient::OnConnect(const boost::system::error_code & errorCode,
> boost::asio::ip::tcp::resolver::iterator endpoint)
> {
> if (errorCode || endpoint == boost::asio::ip::tcp::resolver::iterator())

I think the documentation says somewhere that you either get an error,
or an iterator to a non-empty range. I.e. you either get a list of
resolved addresses, or an error.

> {
> // Error - An error occured while attempting to connect
...
> std::ostringstream msg;
> msg << "An error occured while attempting to connect to " << endpoint->host_name()
> << ". Error code: " << errorCode
> << ". Error Message: " << ErrorCodeToString(errorCode);

I have only used async_connect() once, but this seems a bit fishy. It
looks analogous to (using normal iterators)

if(iter==container.end()) {
do_something_to(iter->foo);
}

which you'd never write.

You may have to pass enough context in some other way, so that you can
formulate a useful error message.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
0 new messages