Просмотр исходного кода

fix : code patch
1. fixed code about delete char*
2. free curl_slist
3. added lock for EndpointProvider::LoadLocalEndpoints()

wb-hx510875 6 лет назад
Родитель
Сommit
f16150baca
3 измененных файлов с 18 добавлено и 1 удалено
  1. 1 0
      core/src/CurlHttpClient.cc
  2. 16 0
      core/src/EndpointProvider.cc
  3. 1 1
      core/src/HttpMessage.cc

+ 1 - 0
core/src/CurlHttpClient.cc

@@ -221,6 +221,7 @@ CurlHttpClient::makeRequest(const HttpRequest &request) {
           HttpMethodToString(request.method()) + " " + request.url().toString()));
     }
   }
+  curl_slist_free_all(list);
 }
 
 }  // namespace AlibabaCloud

+ 16 - 0
core/src/EndpointProvider.cc

@@ -19,6 +19,9 @@
 #include <iomanip>
 #include <json/json.h>
 #include <sstream>
+#include <thread>
+#include <mutex>
+#include <condition_variable>
 
 #ifndef WIN32
 #include "LocalEndpoints.h"
@@ -39,7 +42,10 @@ namespace
 #include <strings.h>
 #endif
 
+std::mutex mutex;
+std::condition_variable cv;
 bool local_endpoints_loaded = false;
+bool local_endpoints_loading = false;
 typedef std::string productType;
 typedef std::string regionType;
 typedef std::string endpointType;
@@ -60,6 +66,8 @@ static void LoadLocalEndpoints()
 {
   Json::Reader reader;
   Json::Value value;
+  std::unique_lock<std::mutex> lock(mutex);
+
   if (local_endpoints_loaded)
   {
     return;
@@ -76,6 +84,10 @@ static void LoadLocalEndpoints()
     return;
   }
 
+  cv.wait(lock, [] { return !local_endpoints_loading; });// continue if loading completed
+
+  local_endpoints_loading = true;
+
   auto regions = value["regions"];
   for (const auto &region : regions)
   {
@@ -107,6 +119,10 @@ static void LoadLocalEndpoints()
     allLocalEndpoints[product] = p;
   }
   local_endpoints_loaded = true;
+  local_endpoints_loading = false;
+
+  lock.unlock();
+  cv.notify_one();
 }
 
 } // namespace

+ 1 - 1
core/src/HttpMessage.cc

@@ -139,7 +139,7 @@ HttpMessage::HeaderValueType HttpMessage::header(KnownHeader header)const {
 
 void HttpMessage::setBody(const char *data, size_t size) {
   if (body_)
-    delete body_;
+    delete[] body_;
   body_ = nullptr;
   bodySize_ = 0;
   if (size) {