deadline_timer回调调用已删除的对象

时间:2013-07-14 08:43:17

标签: c++ boost-asio

在流动的代码中,即使我从boost :: enable_shared_from_this继承了net类,当net get删除时,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;
}

1 个答案:

答案 0 :(得分:1)

您正在使用 boost :: asio :: placeholders :: error 设置回调 OnTimer ,但忽略其错误代码。您应该真正获取并使用此错误代码,或者您将访问已删除的net实例的成员(变量atimer_):

void net::OnTimer(const boost::system::error_code& e)
{
    if( e == boost::asio::error::operation_aborted ) return; //Do nothing if timer is cancelled
    if( e ) return; //Do nothing in case of error
    a = 10;
    timer_.async_wait(boost::bind(&net::OnTimer, 
        shared_from_this, boost::asio::placeholders::error));
}