Boost logo

Boost Users :

Subject: Re: [Boost-users] How to design proper release of a boost::asio socket or wrapper thereof
From: Bjorn Reese (breese_at_[hidden])
Date: 2017-04-05 22:00:24


On 04/05/2017 10:32 PM, Christopher Pisz via Boost-users wrote:

> The problem I have with this example listing is that it cheats and it cheats
> big, by making the tcp_connection a shared_ptr, such that it doesn't worry
> about the lifetime management of each connection.

That is not cheating.

> Consider we'd have a callstack that looked something like this:
> tcp_connection::~tcp_connection
> tcp_server::OnDisconnect
> tcp_connection::OnDisconnect
> tcp_connection::Disconnect

If Disconnect is an asynchronous operation, then it should not call
OnDisconnect directly. You can post it on the io_service instead.

> I imagine everyone doing server programming comes across this scenario in
> some fashion. What is a strategy for handling it?

Preferably shared_ptr, or io_service::post() if for some reason you
cannot use shared_ptr, e.g. if the object has to be destroyed by a
a specific thread.


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