From 7257e61e3de05906ae0c6db691de5e7ba77f5f8a Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 3 Jul 2022 09:29:39 +0300 Subject: [PATCH 55/55] Free curl handle See osdn #44945 Signed-off-by: Marko Lindqvist --- common/fc_interface.c | 2 ++ utility/netfile.c | 31 ++++++++++++++++++++++--------- utility/netfile.h | 7 ++++++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/common/fc_interface.c b/common/fc_interface.c index b7e63a2630..b1741a7ec4 100644 --- a/common/fc_interface.c +++ b/common/fc_interface.c @@ -16,6 +16,7 @@ #endif /* utility */ +#include "netfile.h" #include "shared.h" /* common */ @@ -84,5 +85,6 @@ void free_libfreeciv(void) free_freeciv_storage_dir(); free_user_home_dir(); free_fileinfo_data(); + netfile_free(); fc_strAPI_free(); } diff --git a/utility/netfile.c b/utility/netfile.c index 99ae16ffb7..b8dce3c2b7 100644 --- a/utility/netfile.c +++ b/utility/netfile.c @@ -41,28 +41,28 @@ typedef size_t (*netfile_write_cb)(char *ptr, size_t size, size_t nmemb, void *u static char error_buf_curl[CURL_ERROR_SIZE]; +/* Consecutive transfers can use same handle for better performance */ +static CURL *chandle = NULL; + /*******************************************************************//** Set handle to usable state. ***********************************************************************/ static CURL *netfile_init_handle(void) { - /* Consecutive transfers can use same handle for better performance */ - static CURL *handle = NULL; - - if (handle == NULL) { - handle = curl_easy_init(); + if (chandle == NULL) { + chandle = curl_easy_init(); } else { - curl_easy_reset(handle); + curl_easy_reset(chandle); } error_buf_curl[0] = '\0'; - curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, error_buf_curl); + curl_easy_setopt(chandle, CURLOPT_ERRORBUFFER, error_buf_curl); #ifdef CUSTOM_CACERT_PATH - curl_easy_setopt(handle, CURLOPT_CAINFO, CUSTOM_CACERT_PATH); + curl_easy_setopt(chandle, CURLOPT_CAINFO, CUSTOM_CACERT_PATH); #endif /* CUSTOM_CERT_PATH */ - return handle; + return chandle; } /*******************************************************************//** @@ -284,3 +284,16 @@ bool netfile_send_post(const char *URL, struct netfile_post *post, } #endif /* __EMSCRIPTEN__ */ + +/*******************************************************************//** + Free resources reserved by the netfile system +***********************************************************************/ +void netfile_free(void) +{ +#ifndef __EMSCRIPTEN__ + if (chandle != NULL) { + curl_easy_cleanup(chandle); + chandle = NULL; + } +#endif /* __EMSCRIPTEN__ */ +} diff --git a/utility/netfile.h b/utility/netfile.h index b3782a143b..a3c4b7bc7d 100644 --- a/utility/netfile.h +++ b/utility/netfile.h @@ -1,4 +1,4 @@ -/********************************************************************** +/*********************************************************************** Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,6 +18,9 @@ extern "C" { #endif /* __cplusplus */ +/* utility */ +#include "support.h" /* bool */ + struct netfile_post; struct netfile_write_cb_data @@ -45,6 +48,8 @@ bool netfile_send_post(const char *URL, struct netfile_post *post, FILE *reply_fp, struct netfile_write_cb_data *mem_data, const char *addr); +void netfile_free(void); + #ifdef __cplusplus } #endif /* __cplusplus */ -- 2.35.1