Boost logo

Boost :

Subject: [boost] [ASIO] example clarification (how to)
From: Gaetano Mendola (mendola_at_[hidden])
Date: 2013-12-21 16:09:32


In the asio chat example

http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp11/chat/chat_server.cpp

the class chat_server implements the async_accept handler with a lambda
without use the shared_from_this pattern, isn't an error destroy the
class while the handler is running? Maybe is not a problem looking at
how is used inside the main but without having knowledge on how is it
going to be used which can be the best way to destroy the chat_server
(or similar code) safely? Is something like the following correct?

chat_server::~chat_server() {
   //Call dispatching a cancel operation
   //in order to be sure to not chain another async_accept
   acceptor_.get_service().dispatch(
       [this](){
         acceptor_.cancel();
       });
}

here I'm supposing a single thread is running io_service.run() otherwise
the strand has to be used.

Regards
Gaetano Mendola

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk