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
This commit is contained in:
Martijn Otto 2018-11-07 09:24:50 +01:00
parent a9e03ebc6a
commit 7c298f5d14
No known key found for this signature in database
GPG Key ID: D2E0D5D0B1D606F9
2 changed files with 10 additions and 6 deletions

View File

@ -155,7 +155,8 @@ namespace net_utils
//this should be the last one, because it could be wait on destructor, while other activities possible on other threads //this should be the last one, because it could be wait on destructor, while other activities possible on other threads
t_protocol_handler m_protocol_handler; t_protocol_handler m_protocol_handler;
//typename t_protocol_handler::config_type m_dummy_config; //typename t_protocol_handler::config_type m_dummy_config;
std::list<boost::shared_ptr<connection<t_protocol_handler> > > m_self_refs; // add_ref/release support size_t m_reference_count = 0; // reference count managed through add_ref/release support
boost::shared_ptr<connection<t_protocol_handler> > m_self_ref; // the reference to hold
critical_section m_self_refs_lock; critical_section m_self_refs_lock;
critical_section m_chunking_lock; // held while we add small chunks of the big do_send() to small do_send_chunk() critical_section m_chunking_lock; // held while we add small chunks of the big do_send() to small do_send_chunk()
critical_section m_shutdown_lock; // held while shutting down critical_section m_shutdown_lock; // held while shutting down

View File

@ -230,7 +230,8 @@ PRAGMA_WARNING_DISABLE_VS(4355)
//_dbg3("[sock " << socket_.native_handle() << "] add_ref 2, m_peer_number=" << mI->m_peer_number); //_dbg3("[sock " << socket_.native_handle() << "] add_ref 2, m_peer_number=" << mI->m_peer_number);
if(m_was_shutdown) if(m_was_shutdown)
return false; return false;
m_self_refs.push_back(self); ++m_reference_count;
m_self_ref = std::move(self);
return true; return true;
CATCH_ENTRY_L0("connection<t_protocol_handler>::add_ref()", false); 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; boost::shared_ptr<connection<t_protocol_handler> > back_connection_copy;
LOG_TRACE_CC(context, "[sock " << socket_.native_handle() << "] release"); LOG_TRACE_CC(context, "[sock " << socket_.native_handle() << "] release");
CRITICAL_REGION_BEGIN(m_self_refs_lock); 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"); 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");
//erasing from container without additional copy can cause start deleting object, including m_self_refs // is this the last reference?
back_connection_copy = m_self_refs.back(); if (--m_reference_count == 0) {
m_self_refs.pop_back(); // 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(); CRITICAL_REGION_END();
return true; return true;
CATCH_ENTRY_L0("connection<t_protocol_handler>::release()", false); CATCH_ENTRY_L0("connection<t_protocol_handler>::release()", false);