How to design proper release of a boost::asio socket or wrapper thereof
I am making a few attempts at making my own simple asynch TCP server using boost::asio after not having touched it for several years. The latest example listing I can find is: http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/tutorial/tutdaytime... 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. I am worried about the lifetime management of each connection. I figure the natural thing to do would be to keep some collection of tcp_connection objects or pointers to them inside tcp_server. Adding to that collection from the OnConnect callback and removing from that collection OnDisconnect. Note that OnDisconnect would most likely be called from an actual Disconnect method, which in turn would be called from OnReceive callback or OnSend callback, in the case of an error. Well, therein lies the problem. Consider we'd have a callstack that looked something like this: tcp_connection::~tcp_connection tcp_server::OnDisconnect tcp_connection::OnDisconnect tcp_connection::Disconnect tcp_connection::OnReceive This would cause errors as the call stack unwinds and we are executing code in a object that has had its destructor called...I think, right? I then thought to myself, well I could flag the tcp_connection as disconnected when it disconnects and then on some timer on the tcp_server, go through its collection and start deleting disconnected tcp_connection objects, but then the timer could fire after the flag is set, but before it returns from a OnReceive where an error occurred and I Disconnected as a response. I imagine everyone doing server programming comes across this scenario in some fashion. What is a strategy for handling it? I hope the explanation is good enough to follow. If not let me know and I will create my own source listing, but it will be very large. -- View this message in context: http://boost.2283326.n4.nabble.com/How-to-design-proper-release-of-a-boost-a... Sent from the Boost - Users mailing list archive at Nabble.com.
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.
What's wrong with how connections are managed in the chat_server example? http://think-async.com/Asio/boost_asio_1_10_6/doc/html/boost_asio/example/cp...
participants (2)
-
Arash Partow
-
Christopher Pisz