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

View File

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

View File

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

View File

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