On Mon, Aug 12, 2019 at 8:57 AM TomJordan via Boost-users
Is this shared_ptr approach the idiomatic or even the only way to manage lifetimes in ASIO async calls?
It is not the only way, but by far the simplest and safest - once you achieve a grasp of how asynchrony works and how to manage lifetimes.
It just looks a bit ugly to me, bloats the code a bit and I was wondering if there is any other way to do it without the intrusion of shared_ptrs into the mix?
My advice: get over it. There is nothing wrong with `shared_ptr` and it is the most natural way to manage objects whose lifetime is not deterministic. For example if you perform an asynchronous read and an asynchronous write with the same object, there's no way to know which operation will finish last. Shared-ownership (i.e. `shared_ptr`) is the right tool here. This video and the example code that is used in the video should give you a running start on learning asio and asynchronous programming. It explains how to use shared_ptr in detail to manage lifetimes. https://www.youtube.com/watch?v=7FQwAjELMek Added bonus: after watching the video, you might quickly get rich. Regards