Avoid boost::bind()ing a shared_ptr.
authorCarl Hetherington <cth@carlh.net>
Tue, 22 Feb 2022 19:34:08 +0000 (20:34 +0100)
committerCarl Hetherington <cth@carlh.net>
Fri, 25 Feb 2022 07:00:51 +0000 (08:00 +0100)
src/lib/encode_server_finder.cc
src/lib/encode_server_finder.h

index 6b288d70d75af881f5fe80d6e7b441fe16a80cdf..200286fd94426e945c32cff7c37b8dc0867a47bd 100644 (file)
@@ -203,16 +203,17 @@ catch (...)
 void
 EncodeServerFinder::start_accept ()
 {
-       auto socket = make_shared<Socket>();
+       _accept_socket = make_shared<Socket>();
+
        _listen_acceptor->async_accept (
-               socket->socket(),
-               boost::bind(&EncodeServerFinder::handle_accept, this, boost::asio::placeholders::error, socket)
+               _accept_socket->socket(),
+               boost::bind(&EncodeServerFinder::handle_accept, this, boost::asio::placeholders::error)
                );
 }
 
 
 void
-EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Socket> socket)
+EncodeServerFinder::handle_accept (boost::system::error_code ec)
 {
        if (ec) {
                start_accept ();
@@ -220,18 +221,17 @@ EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Sock
        }
 
        uint32_t length;
-       socket->read (reinterpret_cast<uint8_t*>(&length), sizeof(uint32_t));
+       _accept_socket->read (reinterpret_cast<uint8_t*>(&length), sizeof(uint32_t));
        length = ntohl (length);
 
        scoped_array<char> buffer(new char[length]);
-       socket->read (reinterpret_cast<uint8_t*>(buffer.get()), length);
+       _accept_socket->read (reinterpret_cast<uint8_t*>(buffer.get()), length);
 
        string s (buffer.get());
        auto xml = make_shared<cxml::Document>("ServerAvailable");
        xml->read_string (s);
 
-       auto const ip = socket->socket().remote_endpoint().address().to_string();
-       }
+       auto const ip = _accept_socket->socket().remote_endpoint().address().to_string();
        bool changed = false;
        {
                boost::mutex::scoped_lock lm (_servers_mutex);
index efb498880c6dd07ef4b235126e9c9db42f79c7a8..f8a30af54b295f9a3d6906bd5ab3b878ee6fe805 100644 (file)
@@ -67,7 +67,7 @@ private:
        void listen_thread ();
 
        void start_accept ();
-       void handle_accept (boost::system::error_code ec, std::shared_ptr<Socket> socket);
+       void handle_accept (boost::system::error_code ec);
 
        void config_changed (Config::Property what);
 
@@ -88,5 +88,7 @@ private:
        boost::condition _search_condition;
        boost::mutex _search_condition_mutex;
 
+       std::shared_ptr<Socket> _accept_socket;
+
        static EncodeServerFinder* _instance;
 };