Boost logo

Boost Users :

From: Wang Yun (a_hao_at_[hidden])
Date: 2006-09-14 07:31:26


Hi all,

I have a class

class Connection
{
...
    ~Connection()
    {
        _socket.close();
    }

    void receiveHandler()
    {
        // will crash
    }

    tcp::socket _socket;
}

io_service.run() is called in a working thread, and there are many other
sockets runing on the io_service.
I call async_read on the _socket, and bind Connection::receiveHandler()
as the handler function.
Then I delete the Connection object in main thread, the receive
operation will be cancelled and the receiveHandler()
will be called with errcode boost::asio::error::operation_aborted, but
at that time, the Connection object
has been deleted, calling receiveHandler() will crash if it access the
object's data member.

Because there are still other sockets running on the io_service, I can't
just join the working thread in Connection's destructor.

I want to ensure the socket is closed completely and no more handler
will be called in the destructor.
One method is create an event before calling _socket.close(), then wait
for the event, in receiveHandler(), set the event.
Are there any easier method to do this?

Thanks,
Wang Yun


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net