aboutsummaryrefslogtreecommitdiff
path: root/contrib/epee/include/net/abstract_tcp_server2.inl
diff options
context:
space:
mode:
authorMartijn Otto <git@martijnotto.nl>2018-11-07 09:24:50 +0100
committerMartijn Otto <git@martijnotto.nl>2018-11-07 11:21:52 +0100
commit7c298f5d14e9ef47b812983fbb8a8f446b536fb5 (patch)
tree4f8ab5ae62c9c8e3bf3da57d29bb5a42b3397c8e /contrib/epee/include/net/abstract_tcp_server2.inl
parentMerge pull request #4809 (diff)
downloadmonero-7c298f5d14e9ef47b812983fbb8a8f446b536fb5.tar.xz
No longer use a list for registering self references in the abstract tcp
server Updated assert message Use a local variable that won't destruct at the end of the if-branch Updated comment
Diffstat (limited to 'contrib/epee/include/net/abstract_tcp_server2.inl')
-rw-r--r--contrib/epee/include/net/abstract_tcp_server2.inl13
1 files changed, 8 insertions, 5 deletions
diff --git a/contrib/epee/include/net/abstract_tcp_server2.inl b/contrib/epee/include/net/abstract_tcp_server2.inl
index 9b03941ee..a74eb1f26 100644
--- a/contrib/epee/include/net/abstract_tcp_server2.inl
+++ b/contrib/epee/include/net/abstract_tcp_server2.inl
@@ -230,7 +230,8 @@ PRAGMA_WARNING_DISABLE_VS(4355)
//_dbg3("[sock " << socket_.native_handle() << "] add_ref 2, m_peer_number=" << mI->m_peer_number);
if(m_was_shutdown)
return false;
- m_self_refs.push_back(self);
+ ++m_reference_count;
+ m_self_ref = std::move(self);
return true;
CATCH_ENTRY_L0("connection<t_protocol_handler>::add_ref()", false);
}
@@ -242,10 +243,12 @@ PRAGMA_WARNING_DISABLE_VS(4355)
boost::shared_ptr<connection<t_protocol_handler> > back_connection_copy;
LOG_TRACE_CC(context, "[sock " << socket_.native_handle() << "] release");
CRITICAL_REGION_BEGIN(m_self_refs_lock);
- CHECK_AND_ASSERT_MES(m_self_refs.size(), false, "[sock " << socket_.native_handle() << "] m_self_refs empty at connection<t_protocol_handler>::release() call");
- //erasing from container without additional copy can cause start deleting object, including m_self_refs
- back_connection_copy = m_self_refs.back();
- m_self_refs.pop_back();
+ CHECK_AND_ASSERT_MES(m_reference_count, false, "[sock " << socket_.native_handle() << "] m_reference_count already at 0 at connection<t_protocol_handler>::release() call");
+ // is this the last reference?
+ if (--m_reference_count == 0) {
+ // move the held reference to a local variable, keeping the object alive until the function terminates
+ std::swap(back_connection_copy, m_self_ref);
+ }
CRITICAL_REGION_END();
return true;
CATCH_ENTRY_L0("connection<t_protocol_handler>::release()", false);