Skip to content

Commit 27ee115

Browse files
clarkokClarkok Zhang
andauthored
Fix android getaddrinfo issue (#2273)
Co-authored-by: Clarkok Zhang <clarkok8@gmail.com>
1 parent 5988275 commit 27ee115

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
@@ -3835,22 +3835,30 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service,
38353835
// Fallback implementation using thread-based timeout for other Unix systems
38363836

38373837
struct GetAddrInfoState {
3838+
~GetAddrInfoState() {
3839+
if (info) { freeaddrinfo(info); }
3840+
}
3841+
38383842
std::mutex mutex;
38393843
std::condition_variable result_cv;
38403844
bool completed = false;
38413845
int result = EAI_SYSTEM;
3842-
std::string node = node;
3843-
std::string service = service;
3844-
struct addrinfo hints = hints;
3846+
std::string node;
3847+
std::string service;
3848+
struct addrinfo hints;
38453849
struct addrinfo *info = nullptr;
38463850
};
38473851

38483852
// Allocate on the heap, so the resolver thread can keep using the data.
38493853
auto state = std::make_shared<GetAddrInfoState>();
3854+
state->node = node;
3855+
state->service = service;
3856+
state->hints = *hints;
38503857

3851-
std::thread resolve_thread([=]() {
3852-
auto thread_result = getaddrinfo(
3853-
state->node.c_str(), state->service.c_str(), hints, &state->info);
3858+
std::thread resolve_thread([state]() {
3859+
auto thread_result =
3860+
getaddrinfo(state->node.c_str(), state->service.c_str(), &state->hints,
3861+
&state->info);
38543862

38553863
std::lock_guard<std::mutex> lock(state->mutex);
38563864
state->result = thread_result;
@@ -3868,6 +3876,7 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service,
38683876
// Operation completed within timeout
38693877
resolve_thread.join();
38703878
*res = state->info;
3879+
state->info = nullptr; // Pass ownership to caller
38713880
return state->result;
38723881
} else {
38733882
// Timeout occurred

0 commit comments

Comments
 (0)