在流动的代码中,即使我从 boost::enable_shared_from_this 继承了 net 类,当 net 被删除时,OnTimer 仍然会再次调用一个无效对象。如何解决这个问题?提前致谢。
boost::shared_ptr<boost::asio::io_service> service =
boost::make_shared<boost::asio::io_service>();
class net:public boost::enable_shared_from_this<net>
{
public:
net(boost::shared_ptr<boost::asio::io_service>& service);
void StartTimer();
void OnTimer(const boost::system::error_code& e);
~net();
private:
boost::asio::deadline_timer timer_;
int a;
};
net::net(boost::shared_ptr<boost::asio::io_service>& service)
:timer_(*service, boost::posix_time::milliseconds(1000))
{
}
net::~net()
{
timer_.cancel();
}
void net::StartTimer()
{
timer_.async_wait(boost::bind(&net::OnTimer,
shared_from_this(), boost::asio::placeholders::error));
}
void net::OnTimer(const boost::system::error_code& e)
{
a = 10;
timer_.async_wait(boost::bind(&net::OnTimer,
shared_from_this(), boost::asio::placeholders::error));
}
unsigned int WINAPI ThreadMain(void* arg)
{
boost::shared_ptr<net> ptr(new net(service));
ptr->StartTimer();
Sleep(5000);
boost::shared_ptr<net> invalid_ptr;
ptr = invalid_ptr;
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
boost::shared_ptr<boost::asio::io_service::work> io_service_work =
boost::make_shared<boost::asio::io_service::work>(*service);
boost::system::error_code ec;
HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadMain, NULL, 0, NULL);
if (hThread == NULL)
{
return -1;
}
boost::shared_ptr<boost::thread> thread_ =
boost::make_shared<boost::thread>(boost::bind(&boost::asio::io_service::run, service.get(), ec));
// in order to make sure OnTimer can be called at least once
Sleep(2000);
boost::shared_ptr<boost::asio::io_service::work> invalid_ptr;
io_service_work = invalid_ptr;
thread_->join();
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
return 0;
}