-1

在流动的代码中,即使我从 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;
}
4

1 回答 1

1

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

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));
}
于 2013-07-14T09:34:14.270 回答