From bce3071a126b1c203d2da4496ea55cc0b711d6c0 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Sun, 6 Jan 2019 18:27:26 -0800 Subject: [PATCH] DNSLookup: fix #8 --- ixwebsocket/IXDNSLookup.cpp | 13 ++++++++----- ixwebsocket/IXDNSLookup.h | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/ixwebsocket/IXDNSLookup.cpp b/ixwebsocket/IXDNSLookup.cpp index 65b5208d..a5608b07 100644 --- a/ixwebsocket/IXDNSLookup.cpp +++ b/ixwebsocket/IXDNSLookup.cpp @@ -105,7 +105,7 @@ namespace ix // Good resource on thread forced termination // https://www.bo-yang.net/2017/11/19/cpp-kill-detached-thread // - _thread = std::thread(&DNSLookup::run, this, _hostname, _port); + _thread = std::thread(&DNSLookup::run, this, _id, _hostname, _port); _thread.detach(); std::unique_lock lock(_conditionVariableMutex); @@ -138,11 +138,13 @@ namespace ix return _res; } - void DNSLookup::run(const std::string& hostname, int port) // thread runner + void DNSLookup::run(uint64_t id, const std::string& hostname, int port) // thread runner { - uint64_t id = _id; + // We don't want to read or write into members variables of an object that could be + // gone, so we use temporary variables (res) or we pass in by copy everything that + // getAddrInfo needs to work. std::string errMsg; - _res = getAddrInfo(hostname, port, errMsg); + struct addrinfo* res = getAddrInfo(hostname, port, errMsg); // if this isn't an active job, and the control thread is gone // there is not thing to do, and we don't want to touch the defunct @@ -153,9 +155,10 @@ namespace ix return; } + // Copy result into the member variables + _res = res; _errMsg = errMsg; _condition.notify_one(); _done = true; } - } diff --git a/ixwebsocket/IXDNSLookup.h b/ixwebsocket/IXDNSLookup.h index dac3d52f..993307d7 100644 --- a/ixwebsocket/IXDNSLookup.h +++ b/ixwebsocket/IXDNSLookup.h @@ -43,7 +43,7 @@ namespace ix int port, std::string& errMsg); - void run(const std::string& hostname, int port); // thread runner + void run(uint64_t id, const std::string& hostname, int port); // thread runner std::string _hostname; int _port;