Is it safe to destroy a socket object while asyn_read can happen in boost.ASIO?

In the following code:

tcp::socket socket(io_service);
tcp::endpoint ep(boost::asio::ip::address::from_string(addr), i);


socket.async_connect(ep, &connect_handler);

socket.close();

Is it correct to close the socket object or close it only in connect_handler(), resort to shared_ptr to extend the life of the socket object? Thank.

+3
source share
3 answers

Closing the socket is not a problem, but the socket is destroyed and freed. One way to deal with it is to simply make sure that the socket survives the io_service where the work is done. In other words, you just remember to remove it until io_service comes out. Obviously, this will not work in any situation.

, , io_service, ASIO , . shared_ptr, , io_service.

, , .

+3

. connect_handler ec == boost:: asio:: error:: connection_aborted. , io_service.run() .

+1

Chila, , . , / , . , connection_aborted.

shared_ptr, , , , . , , , . , , , /dealloc. , , , , (. ).

+1
source

Source: https://habr.com/ru/post/1735198/


All Articles