Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent XMLHttpRequest from being gc incorrectly, when timeout occurs. #1724

Merged
merged 6 commits into from Apr 22, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 19 additions & 8 deletions cocos/scripting/js-bindings/manual/jsb_xmlhttprequest.cpp
Expand Up @@ -214,6 +214,7 @@ class XMLHttpRequest : public Ref
bool _isLoadStart;
bool _isLoadEnd;
bool _isDiscardedByReset;
bool _isTimeout;
};

XMLHttpRequest::XMLHttpRequest()
Expand All @@ -235,6 +236,7 @@ XMLHttpRequest::XMLHttpRequest()
, _isLoadStart(false)
, _isLoadEnd(false)
, _isDiscardedByReset(false)
, _isTimeout(false)
{
}

Expand Down Expand Up @@ -271,6 +273,7 @@ bool XMLHttpRequest::open(const std::string& method, const std::string& url)

_status = 0;
_isAborted = false;
_isTimeout = false;

setReadyState(ReadyState::OPENED);

Expand Down Expand Up @@ -326,6 +329,9 @@ void XMLHttpRequest::abort()
}

_readyState = ReadyState::UNSENT;

//request is aborted, no more callback needed.
_httpRequest->setResponseCallback(nullptr);
}

void XMLHttpRequest::setReadyState(ReadyState readyState)
Expand Down Expand Up @@ -399,7 +405,16 @@ void XMLHttpRequest::getHeader(const std::string& header)
void XMLHttpRequest::onResponse(HttpClient* client, HttpResponse* response)
{
Application::getInstance()->getScheduler()->unscheduleAllForTarget(this);


if(_isTimeout) {
_isLoadEnd = true;
if(onloadend)
{
onloadend();
}
return;
}

if (_isAborted || _readyState == ReadyState::UNSENT)
{
return;
Expand Down Expand Up @@ -497,18 +512,14 @@ std::string XMLHttpRequest::getMimeType() const

void XMLHttpRequest::sendRequest()
{
_isTimeout = false;
PatriceJiang marked this conversation as resolved.
Show resolved Hide resolved
if (_timeoutInMilliseconds > 0)
{
Application::getInstance()->getScheduler()->schedule([this](float dt){
if (ontimeout != nullptr)
ontimeout();

_isTimeout = true;
_readyState = ReadyState::UNSENT;

_isLoadEnd = true;
if (onloadend != nullptr)
onloadend();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里会导致对象被unroot, 有概率在onRespone之前被析构.

onloadend的会调移到onResponse内以修复.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onResponse() 一定会被调用吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 在超时的情况下 是一定会被调用的


}, this, _timeoutInMilliseconds / 1000.0f, 0, 0.0f, false, "XMLHttpRequest");
}
setHttpRequestHeader();
Expand Down Expand Up @@ -650,7 +661,7 @@ static bool XMLHttpRequest_constructor(se::State& s)
func.toObject()->call(se::EmptyValueArray, thizObj);
}
};

request->onloadstart = [=](){
if (!request->isDiscardedByReset())
{
Expand Down