From d7640b68d21cb6471cf1956f731a874ce5eaffcb Mon Sep 17 00:00:00 2001 From: sicxs Date: Mon, 27 Nov 2017 16:21:25 +0800 Subject: [PATCH] change receivers to share_ptr change receivers to share_ptr --- src/net/include/scy/net/socketadapter.h | 4 +++- src/net/src/socketadapter.cpp | 11 +++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/net/include/scy/net/socketadapter.h b/src/net/include/scy/net/socketadapter.h index 61ffdfb84..a7e2aaab2 100644 --- a/src/net/include/scy/net/socketadapter.h +++ b/src/net/include/scy/net/socketadapter.h @@ -106,12 +106,14 @@ class Net_API SocketAdapter struct Ref { + typedef std::shared_ptr ptr_t; + SocketAdapter* ptr; bool alive; }; SocketAdapter* _sender; - std::vector _receivers; + std::vector _receivers; bool _dirty = false; }; diff --git a/src/net/src/socketadapter.cpp b/src/net/src/socketadapter.cpp index 2fdea5e24..a5908a7d6 100644 --- a/src/net/src/socketadapter.cpp +++ b/src/net/src/socketadapter.cpp @@ -192,7 +192,10 @@ void SocketAdapter::addReceiver(SocketAdapter* adapter) // Note that we insert new adapters in the back of the queue, // and iterate in reverse to ensure calling order is preserved. _dirty = true; - _receivers.push_back(new Ref{ adapter, true }); + auto ptrRef = std::make_shared(); + ptrRef->ptr = adapter; + ptrRef->alive = true; + _receivers.push_back(ptrRef); // _receivers.insert(_receivers.begin(), new Ref{ adapter, false }); // insert front // std::sort(_receivers.begin(), _receivers.end(), // [](SocketAdapter const& l, SocketAdapter const& r) { @@ -204,7 +207,7 @@ void SocketAdapter::removeReceiver(SocketAdapter* adapter) { assert(adapter != this); auto it = std::find_if(_receivers.begin(), _receivers.end(), - [&](const Ref* ref) { return ref->ptr == adapter; }); + [&](const Ref::ptr_t& ref) { return ref->ptr == adapter; }); if (it != _receivers.end()) { (*it)->alive = false; } } @@ -215,7 +218,7 @@ void SocketAdapter::cleanupReceivers() for (auto it = _receivers.begin(); it != _receivers.end();) { auto ref = *it; if (!ref->alive) { - delete ref; + //delete ref; it = _receivers.erase(it); } else ++it; @@ -234,7 +237,7 @@ std::vector SocketAdapter::receivers() { std::vector items; std::transform(_receivers.begin(), _receivers.end(), std::back_inserter(items), - [](const Ref* ref) { return ref->ptr; }); + [](const Ref::ptr_t& ref) { return ref->ptr; }); return items; }