Using self defined function to replace strdup (#4046)

This commit is contained in:
timlyeee
2021-11-17 10:27:12 +08:00
committed by GitHub
parent e53192132b
commit c7efe4e668

View File

@@ -86,9 +86,6 @@ public:
explicit HttpURLConnection(HttpClient *httpClient)
: _client(httpClient),
_httpURLConnection(nullptr),
_requestmethod(""),
_responseCookies(""),
_cookieFileName(""),
_contentLength(0) {
}
@@ -370,7 +367,7 @@ public:
_cookieFileName = filename;
}
int getContentLength() {
int getContentLength() const {
return _contentLength;
}
@@ -549,7 +546,13 @@ private:
return nullptr;
}
std::string strValue = cc::StringUtils::getStringUTFCharsJNI(env, jstr);
return strdup(strValue.c_str());
size_t size = strValue.size() + 1;
char* retVal = static_cast<char *>(malloc(size));
if (retVal == nullptr) {
return nullptr;
}
memcpy(retVal, strValue.c_str(), size);
return retVal;
}
int getCStrFromJByteArray(jbyteArray jba, JNIEnv *env, char **ppData) { //NOLINT(readability-convert-member-functions-to-static)
@@ -570,7 +573,6 @@ private:
return _responseCookies;
}
private:
HttpClient *_client;
jobject _httpURLConnection;
std::string _requestmethod;
@@ -582,7 +584,7 @@ private:
// Process Response
void HttpClient::processResponse(HttpResponse *response, char *responseMessage) {
auto request = response->getHttpRequest();
auto* request = response->getHttpRequest();
HttpRequest::Type requestType = request->getRequestType();
if (HttpRequest::Type::GET != requestType &&
@@ -728,7 +730,7 @@ void HttpClient::networkThread() {
_schedulerMutex.lock();
if (auto sche = _scheduler.lock()) {
sche->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this));
sche->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this)); //NOLINT
}
_schedulerMutex.unlock();
}
@@ -787,7 +789,7 @@ void HttpClient::destroyInstance() {
CC_LOG_DEBUG("HttpClient::destroyInstance ...");
auto thiz = gHttpClient;
auto *thiz = gHttpClient;
gHttpClient = nullptr;
if (auto sche = thiz->_scheduler.lock()) {
@@ -844,7 +846,7 @@ bool HttpClient::lazyInitThreadSemaphore() {
return true;
}
auto t = std::thread(CC_CALLBACK_0(HttpClient::networkThread, this));
auto t = std::thread(CC_CALLBACK_0(HttpClient::networkThread, this)); //NOLINT
t.detach();
_isInited = true;