Opened 12 years ago

Closed 12 years ago

#5285 closed Bugs (worksforme)

problem about boost::asio::ip::tcp::resolver

Reported by: gong yiling <gongyiling3468@…> Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.45.0 Severity: Problem
Keywords: Cc:

Description

one day i found a bug in my project, that's one of async_resolve's callback never called. i checked this problem and repeat this problem as following program:

#include "stdafx.h"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/system/error_code.hpp>
#include <stdio.h>
using namespace boost::asio;
using namespace boost;
class client
{
public:
	client(io_service& ios):
	m_ios(ios),
	m_resolver(ios)
	{
		ip::tcp::resolver* resolver = new ip::tcp::resolver(m_ios);
		delete resolver;
		ip::tcp::resolver::query query("www.baidu.com","80");
		m_resolver.async_resolve(query,boost::bind(&client::handle_resolve,this,_1,_2));
	}
	void handle_resolve(const system::error_code& ec,ip::tcp::resolver_iterator it)
	{
		puts("see me?");
	}
	io_service& m_ios;
	ip::tcp::resolver m_resolver;
};

int _tmain(int argc, _TCHAR* argv[])
{
	io_service ios;
	client* c = new client(ios);
	ios.run();
	return 0;
}

i digged into the source code( in function boost.asio.detail.resolver_service.destroy ) it seems when i delete a temporary created resolver, the resolver's work service will be stopped. and thus client::handle_resolve will never be called.

thx

Change History (1)

comment:1 by chris_kohlhoff, 12 years ago

Resolution: worksforme
Status: newclosed

I cannot reproduce any issue with 1.45 or the trunk. Note that you are leaking the client object. You need to destroy it before returning from main(). If you still experience the issue after fixing that, please supply more detailed information about how you build the program.

N.B. to the best of my knowledge, the resolver_service::destroy() function has always been empty.

Note: See TracTickets for help on using tickets.