From 5f8356741e48bb27f1d6a5c67951f2904194299c Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 18 Apr 2015 13:55:15 -0400 Subject: [PATCH] fixed potential memory leak --- SSU.cpp | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/SSU.cpp b/SSU.cpp index 08dd7f9d..9c12441a 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -217,24 +217,33 @@ namespace transport for (auto it1: packets) { auto packet = it1; - if (!session || session->GetRemoteEndpoint () != packet->from) // we received packet for other session than previous - { - if (session) session->FlushData (); - auto it = m_Sessions.find (packet->from); - if (it != m_Sessions.end ()) - session = it->second; - if (!session) + try + { + if (!session || session->GetRemoteEndpoint () != packet->from) // we received packet for other session than previous { - session = std::make_shared (*this, packet->from); - session->WaitForConnect (); + if (session) session->FlushData (); + auto it = m_Sessions.find (packet->from); + if (it != m_Sessions.end ()) + session = it->second; + if (!session) { - std::unique_lock l(m_SessionsMutex); - m_Sessions[packet->from] = session; - } - LogPrint ("New SSU session from ", packet->from.address ().to_string (), ":", packet->from.port (), " created"); + session = std::make_shared (*this, packet->from); + session->WaitForConnect (); + { + std::unique_lock l(m_SessionsMutex); + m_Sessions[packet->from] = session; + } + LogPrint (eLogInfo, "New SSU session from ", packet->from.address ().to_string (), ":", packet->from.port (), " created"); + } } - } - session->ProcessNextMessage (packet->buf, packet->len, packet->from); + session->ProcessNextMessage (packet->buf, packet->len, packet->from); + } + catch (std::exception& ex) + { + LogPrint (eLogError, "SSU: HandleReceivedPackets ", ex.what ()); + if (session) session->FlushData (); + session = nullptr; + } delete packet; } if (session) session->FlushData ();