DEV9: Use std::unique_ptr for tracking active pings

This commit is contained in:
TheLastRar 2024-07-12 18:21:47 +01:00 committed by lightningterror
parent 9da3bccca2
commit 48a1ec3531
2 changed files with 6 additions and 14 deletions

View File

@ -685,7 +685,7 @@ namespace Sessions
pingRet = pings[i]->Recv(); pingRet = pings[i]->Recv();
if (pingRet != nullptr) if (pingRet != nullptr)
{ {
Ping* ping = pings[i]; std::unique_ptr<Ping> ping = std::move(pings[i]);
// Remove ping from list and unlock mutex // Remove ping from list and unlock mutex
pings.erase(pings.begin() + i); pings.erase(pings.begin() + i);
lock.unlock(); lock.unlock();
@ -729,9 +729,6 @@ namespace Sessions
else else
DevCon.WriteLn("DEV9: ICMP: ICMP timeout"); DevCon.WriteLn("DEV9: ICMP: ICMP timeout");
// Free ping
delete ping;
if (ret.has_value()) if (ret.has_value())
DevCon.WriteLn("DEV9: ICMP: Return Ping"); DevCon.WriteLn("DEV9: ICMP: Return Ping");
@ -848,13 +845,12 @@ namespace Sessions
DevCon.WriteLn("DEV9: ICMP: Send Ping"); DevCon.WriteLn("DEV9: ICMP: Send Ping");
open++; open++;
Ping* ping = new Ping(icmpPayload->GetLength()); std::unique_ptr<Ping> ping = std::make_unique<Ping>(icmpPayload->GetLength());
if (!ping->IsInitialised()) if (!ping->IsInitialised())
{ {
if (--open == 0) if (--open == 0)
RaiseEventConnectionClosed(); RaiseEventConnectionClosed();
delete ping;
return false; return false;
} }
@ -862,7 +858,6 @@ namespace Sessions
{ {
if (--open == 0) if (--open == 0)
RaiseEventConnectionClosed(); RaiseEventConnectionClosed();
delete ping;
return false; return false;
} }
@ -873,7 +868,7 @@ namespace Sessions
{ {
std::scoped_lock lock(ping_mutex); std::scoped_lock lock(ping_mutex);
pings.push_back(ping); pings.push_back(std::move(ping));
} }
break; break;
@ -892,10 +887,8 @@ namespace Sessions
ICMP_Session::~ICMP_Session() ICMP_Session::~ICMP_Session()
{ {
std::scoped_lock lock(ping_mutex);
// Cleanup // Cleanup
for (size_t i = 0; i < pings.size(); i++) std::scoped_lock lock(ping_mutex);
delete pings[i]; pings.clear();
} }
} // namespace Sessions } // namespace Sessions

View File

@ -68,9 +68,8 @@ namespace Sessions
~Ping(); ~Ping();
}; };
SimpleQueue<PacketReader::IP::ICMP::ICMP_Packet*> _recvBuff;
std::mutex ping_mutex; std::mutex ping_mutex;
std::vector<Ping*> pings; std::vector<std::unique_ptr<Ping>> pings;
ThreadSafeMap<Sessions::ConnectionKey, Sessions::BaseSession*>* connections; ThreadSafeMap<Sessions::ConnectionKey, Sessions::BaseSession*>* connections;
std::atomic<int> open{0}; std::atomic<int> open{0};