dimanche 22 décembre 2019

Unexpected invocation of the destructor after an invokation of a product of std::bind

There is a simple example with making use of boost::asio::io_context https://github.com/unegare/boost-ex/blob/500e46f4d3b41e2abe48e2deccfab39d44ae94e0/main.cpp

stdout:

[140487203505984] MyWorker copy constructor
[140487203505984] MyWorker move constructor
[140487185372928] Thread Start
ep: 95.165.130.37:80
[140487203505984] Client with args
HERE: use_count = 2
[140487203505984] Client copy
[140487203505984] Client move
[140487203505984] ~Client buff.use_count: 0 | sock.use_count: 0 | io_context.use_count: 0
[140487185372928] Client move
[140487185372928] ~Client buff.use_count: 0 | sock.use_count: 0 | io_context.use_count: 0
OnConnect
GET / HTTP/1.1
Host: unegare.info


OnConnect use_count: 2
[140487185372928] ~Client buff.use_count: 2 | sock.use_count: 3 | io_context.use_count: 5
Segmentation Fault (core dumped)

But there is a little problem with the understanding of the segfault caused by the bad reference to the Client object.

But I do not understand why cl2 becomes destructed after the call of

sock->async_connect(ep, std::bind(std::mem_fn(&Client::OnConnect), *cl2->shared_from_this(), std::placeholders::_1));

at the 162 line.

As well as ... Why was there an invocation of the copy constructor? as it may be noticed from the stdout above.

Aucun commentaire:

Enregistrer un commentaire