]> git.sesse.net Git - casparcg/commitdiff
Fixed life-time issue in AsyncEventServer shutdown causing undefined behaviour
authorHelge Norberg <helge.norberg@svt.se>
Wed, 11 Nov 2015 14:56:08 +0000 (15:56 +0100)
committerHelge Norberg <helge.norberg@svt.se>
Wed, 11 Nov 2015 14:56:08 +0000 (15:56 +0100)
protocol/util/AsyncEventServer.cpp
protocol/util/AsyncEventServer.h

index c883a78f72f3311f755db83a6813ba1a0e76389e..0a18c8d844f4294cb20f4a2be09524470f890d46 100644 (file)
@@ -293,17 +293,21 @@ struct AsyncEventServer::implementation : public spl::enable_shared_from_this<im
        {
        }
 
-       ~implementation()
+       void stop()
        {
                try
                {
                        acceptor_.cancel();
                        acceptor_.close();
                }
-               catch(...)
+               catch (...)
                {
                        CASPAR_LOG_CURRENT_EXCEPTION();
                }
+       }
+
+       ~implementation()
+       {
                auto conns_set = connection_set_;
 
                service_->post([conns_set]
@@ -317,7 +321,7 @@ struct AsyncEventServer::implementation : public spl::enable_shared_from_this<im
        void start_accept() 
        {
                spl::shared_ptr<tcp::socket> socket(new tcp::socket(*service_));
-               acceptor_.async_accept(*socket, std::bind(&implementation::handle_accept, this, socket, std::placeholders::_1));
+               acceptor_.async_accept(*socket, std::bind(&implementation::handle_accept, shared_from_this(), socket, std::placeholders::_1));
     }
 
        void handle_accept(const spl::shared_ptr<tcp::socket>& socket, const boost::system::error_code& error) 
@@ -353,7 +357,11 @@ AsyncEventServer::AsyncEventServer(
        impl_->start_accept();
 }
 
-AsyncEventServer::~AsyncEventServer() {}
+AsyncEventServer::~AsyncEventServer()
+{
+       impl_->stop();
+}
+
 void AsyncEventServer::add_client_lifecycle_object_factory(const lifecycle_factory_t& factory) { impl_->add_client_lifecycle_object_factory(factory); }
 
 }}
index 8f491dffeee336dc46758e8b71a562eec7f647b7..ac2b33b9d160ca5980947e2f2d312fe90481e0b6 100644 (file)
 #include <common/memory.h>
 
 #include <boost/asio.hpp>
+#include <boost/noncopyable.hpp>
 
 namespace caspar { namespace IO {
 
        typedef std::function<std::pair<std::wstring, std::shared_ptr<void>> (const std::string& ipv4_address)>
                lifecycle_factory_t;
 
-class AsyncEventServer
+class AsyncEventServer : boost::noncopyable
 {
 public:
        explicit AsyncEventServer(