fix all valgrind possible memory loss in async ICMP/TCP ping

This commit is contained in:
DuckVador 2020-07-14 19:15:33 +08:00
parent 21a03aab8d
commit f42ef88cf6
No known key found for this signature in database
GPG Key ID: 9B0DED785B37110E
5 changed files with 47 additions and 16 deletions

View File

@ -1 +1 @@
5779 5780

View File

@ -19,6 +19,8 @@ namespace Qv2ray::components::latency
void LatencyTestThread::pushRequest(const ConnectionId &id, int totalTestCount, Qv2rayLatencyTestingMethod method) void LatencyTestThread::pushRequest(const ConnectionId &id, int totalTestCount, Qv2rayLatencyTestingMethod method)
{ {
if(isStop)
return;
std::unique_lock<std::mutex> lockGuard{ m }; std::unique_lock<std::mutex> lockGuard{ m };
const auto &[protocol, host, port] = GetConnectionInfo(id); const auto &[protocol, host, port] = GetConnectionInfo(id);
requests.emplace_back(LatencyTestRequest{ id, host, port, totalTestCount, method }); requests.emplace_back(LatencyTestRequest{ id, host, port, totalTestCount, method });
@ -31,12 +33,23 @@ namespace Qv2ray::components::latency
stopTimer->on<uvw::TimerEvent>([this](auto &, auto &handle) { stopTimer->on<uvw::TimerEvent>([this](auto &, auto &handle) {
if (isStop) if (isStop)
{ {
handle.stop(); if(!requests.empty())
handle.close(); requests.clear();
requests.clear(); int timer_count=0;
loop->clear(); //LOG(MODULE_NETWORK,"fuck")
loop->close(); loop->walk([&timer_count,this](uvw::BaseHandle&h)
loop->stop(); {
if(!h.closing())
timer_count++;
});
if(timer_count==1)//only current timer
{
handle.stop();
handle.close();
loop->clear();
loop->close();
loop->stop();
}
} }
else else
{ {
@ -76,6 +89,8 @@ namespace Qv2ray::components::latency
} }
void LatencyTestThread::pushRequest(const QList<ConnectionId> &ids, int totalTestCount, Qv2rayLatencyTestingMethod method) void LatencyTestThread::pushRequest(const QList<ConnectionId> &ids, int totalTestCount, Qv2rayLatencyTestingMethod method)
{ {
if(isStop)
return;
std::unique_lock<std::mutex> lockGuard{ m }; std::unique_lock<std::mutex> lockGuard{ m };
for (const auto &id : ids) for (const auto &id : ids)
{ {

View File

@ -115,6 +115,7 @@ namespace Qv2ray::components::latency::tcping
data.errorMessage = e.what(); data.errorMessage = e.what();
notifyTestHost(); notifyTestHost();
h.clear(); h.clear();
h.close();
}); });
tcpClient->once<uvw::ConnectEvent>([ptr = shared_from_this(), start = system_clock::now(), this](auto &, auto &h) { tcpClient->once<uvw::ConnectEvent>([ptr = shared_from_this(), start = system_clock::now(), this](auto &, auto &h) {
++successCount; ++successCount;

View File

@ -144,6 +144,13 @@ namespace Qv2ray::components::latency::icmping
timoutTimer->clear(); timoutTimer->clear();
timoutTimer->close(); timoutTimer->close();
} }
if(pollHandle)
{
if(!pollHandle->closing())
pollHandle->stop();
pollHandle->clear();
pollHandle->close();
}
return true; return true;
} }
return false; return false;
@ -153,17 +160,24 @@ namespace Qv2ray::components::latency::icmping
{ {
timoutTimer = loop->resource<uvw::TimerHandle>(); timoutTimer = loop->resource<uvw::TimerHandle>();
uvw::OSSocketHandle osSocketHandle{ socketId }; uvw::OSSocketHandle osSocketHandle{ socketId };
auto pollHandle = loop->resource<uvw::PollHandle>(osSocketHandle); pollHandle = loop->resource<uvw::PollHandle>(osSocketHandle);
timoutTimer->once<uvw::TimerEvent>([pollHandle,this,ptr=shared_from_this()](auto&,uvw::TimerHandle&h) timoutTimer->once<uvw::TimerEvent>([this,ptr=std::weak_ptr<ICMPPing>{shared_from_this()}](auto&,uvw::TimerHandle&h)
{ {
pollHandle->clear(); if(ptr.expired())
pollHandle->stop(); return;
pollHandle->close(); else
successCount=0; {
data.failedCount=data.totalCount=req.totalCount; auto p=ptr.lock();
notifyTestHost(); pollHandle->clear();
if(!pollHandle->closing())
pollHandle->stop();
pollHandle->close();
successCount = 0;
data.failedCount = data.totalCount = req.totalCount;
notifyTestHost();
}
}); });
timoutTimer->start(uvw::TimerHandle::Time{ 15000 }, uvw::TimerHandle::Time{ 0 }); timoutTimer->start(uvw::TimerHandle::Time{ 10000 }, uvw::TimerHandle::Time{ 0 });
auto pollEvent = uvw::Flags<uvw::PollHandle::Event>::from<uvw::PollHandle::Event::READABLE>(); auto pollEvent = uvw::Flags<uvw::PollHandle::Event>::from<uvw::PollHandle::Event::READABLE>();
pollHandle->on<uvw::PollEvent>([this, ptr = shared_from_this()](uvw::PollEvent &, uvw::PollHandle &h) { pollHandle->on<uvw::PollEvent>([this, ptr = shared_from_this()](uvw::PollEvent &, uvw::PollHandle &h) {
timeval end; timeval end;

View File

@ -76,6 +76,7 @@ namespace Qv2ray::components::latency::icmping
std::shared_ptr<uvw::Loop> loop; std::shared_ptr<uvw::Loop> loop;
std::shared_ptr<uvw::GetAddrInfoReq> getAddrHandle; std::shared_ptr<uvw::GetAddrInfoReq> getAddrHandle;
std::shared_ptr<uvw::TimerHandle> timoutTimer; std::shared_ptr<uvw::TimerHandle> timoutTimer;
std::shared_ptr<uvw::PollHandle> pollHandle;
std::vector<timeval> startTimevals; std::vector<timeval> startTimevals;
QString initErrorMessage; QString initErrorMessage;
}; };