diff --git a/native/cocos/bindings/manual/jsb_websocket.cpp b/native/cocos/bindings/manual/jsb_websocket.cpp index 9683fd9593..b7a25f864f 100644 --- a/native/cocos/bindings/manual/jsb_websocket.cpp +++ b/native/cocos/bindings/manual/jsb_websocket.cpp @@ -184,6 +184,7 @@ void JsbWebSocketDelegate::onClose(cc::network::WebSocket *ws) { //JS Websocket object now can be GC, since the connection is closed. wsObj->unroot(); + _JSDelegate.toObject()->unroot(); // Websocket instance is attached to global object in 'WebSocket_close' // It's safe to detach it here since JS 'onclose' method has been already invoked. @@ -224,8 +225,6 @@ void JsbWebSocketDelegate::onError(cc::network::WebSocket *ws, const cc::network } else { SE_REPORT_ERROR("Can't get onerror function!"); } - - wsObj->unroot(); } void JsbWebSocketDelegate::setJSDelegate(const se::Value &jsDelegate) { @@ -433,7 +432,7 @@ static bool webSocketClose(se::State &s) { CC_ASSERT(false); } } else { - CC_ASSERT(false); + CC_ASSERT(false); } } else { SE_REPORT_ERROR("wrong number of arguments: %d, was expecting <=2", argc); diff --git a/native/cocos/bindings/manual/jsb_xmlhttprequest.cpp b/native/cocos/bindings/manual/jsb_xmlhttprequest.cpp index 3f03f89fbc..81c0b6385d 100644 --- a/native/cocos/bindings/manual/jsb_xmlhttprequest.cpp +++ b/native/cocos/bindings/manual/jsb_xmlhttprequest.cpp @@ -172,6 +172,16 @@ public: bool isDiscardedByReset() const { return _isDiscardedByReset; } + inline void clearCallbacks() { + onloadstart = nullptr; + onload = nullptr; + onloadend = nullptr; + onreadystatechange = nullptr; + onabort = nullptr; + onerror = nullptr; + ontimeout = nullptr; + } + private: ~XMLHttpRequest() override; @@ -225,6 +235,7 @@ XMLHttpRequest::XMLHttpRequest() _httpRequest(ccnew HttpRequest()), _responseType(ResponseType::STRING), _readyState(ReadyState::UNSENT) { + _httpRequest->addRef(); } XMLHttpRequest::~XMLHttpRequest() { @@ -576,6 +587,7 @@ se::Class *__jsb_XMLHttpRequest_class = nullptr; //NOLINT(readability-identifier static bool XMLHttpRequest_finalize(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references) auto *request = static_cast(s.nativeThisObject()); SE_LOGD("XMLHttpRequest_finalize, %p ... \n", request); + request->clearCallbacks(); return true; } SE_BIND_FINALIZE_FUNC(XMLHttpRequest_finalize) diff --git a/native/cocos/network/HttpClient-java.cpp b/native/cocos/network/HttpClient-java.cpp index 7bb6756d31..8723f794a2 100644 --- a/native/cocos/network/HttpClient-java.cpp +++ b/native/cocos/network/HttpClient-java.cpp @@ -833,6 +833,7 @@ HttpClient::HttpClient() _cookie(nullptr), _requestSentinel(ccnew HttpRequest()) { CC_LOG_DEBUG("In the constructor of HttpClient!"); + _requestSentinel->addRef(); if (gThreadPool == nullptr) { gThreadPool = LegacyThreadPool::newFixedThreadPool(4); } diff --git a/native/cocos/network/HttpClient.cpp b/native/cocos/network/HttpClient.cpp index 137b704674..f7f9650b2c 100644 --- a/native/cocos/network/HttpClient.cpp +++ b/native/cocos/network/HttpClient.cpp @@ -365,6 +365,7 @@ HttpClient::HttpClient() _cookie(nullptr), _requestSentinel(ccnew HttpRequest()) { CC_LOG_DEBUG("In the constructor of HttpClient!"); + _requestSentinel->addRef(); if (gThreadPool == nullptr) { gThreadPool = LegacyThreadPool::newFixedThreadPool(4); }