Skip to content

Commit 711f661

Browse files
author
Clarkok Zhang
committed
Fix android getaddrinfo issue
1 parent 4b2b851 commit 711f661

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

httplib.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,22 +3809,30 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service,
38093809
// Fallback implementation using thread-based timeout for other Unix systems
38103810

38113811
struct GetAddrInfoState {
3812+
~GetAddrInfoState() {
3813+
if (info) { freeaddrinfo(info); }
3814+
}
3815+
38123816
std::mutex mutex;
38133817
std::condition_variable result_cv;
38143818
bool completed = false;
38153819
int result = EAI_SYSTEM;
3816-
std::string node = node;
3817-
std::string service = service;
3818-
struct addrinfo hints = hints;
3820+
std::string node;
3821+
std::string service;
3822+
struct addrinfo hints;
38193823
struct addrinfo *info = nullptr;
38203824
};
38213825

38223826
// Allocate on the heap, so the resolver thread can keep using the data.
38233827
auto state = std::make_shared<GetAddrInfoState>();
3828+
state->node = node;
3829+
state->service = service;
3830+
state->hints = *hints;
38243831

3825-
std::thread resolve_thread([=]() {
3826-
auto thread_result = getaddrinfo(
3827-
state->node.c_str(), state->service.c_str(), hints, &state->info);
3832+
std::thread resolve_thread([state]() {
3833+
auto thread_result =
3834+
getaddrinfo(state->node.c_str(), state->service.c_str(), &state->hints,
3835+
&state->info);
38283836

38293837
std::lock_guard<std::mutex> lock(state->mutex);
38303838
state->result = thread_result;
@@ -3842,6 +3850,7 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service,
38423850
// Operation completed within timeout
38433851
resolve_thread.join();
38443852
*res = state->info;
3853+
state->info = nullptr; // Pass ownership to caller
38453854
return state->result;
38463855
} else {
38473856
// Timeout occurred

0 commit comments

Comments
 (0)