Hello, I have a simple Async blueprint node written in c++ that does some http requests every 0.5s to a local server and then calls a MULTICAST_DELEGATE callback.
If the server I’m trying to ping is offline, or takes more time to respond then the interval I’m calling the HTTP request ( say the interval is 0.5s, and the server responds or the timeout is in 1.0s ) I get a crash after a couple of runs.
I imagine it has something to do with the fact that the mRequest->OnProcessRequestComplete().BindLambda
takes a time to run and the Delegate doesn’t exists anymore.
here’s my code:
TSharedPtr<IHttpRequest, ESPMode::ThreadSafe> mRequest = FHttpModule::Get().CreateRequest().ToSharedPtr();
mRequest->SetVerb(mVerb);
mRequest->SetTimeout(0.4f); // if this is more then the interval you are calling this function, you will get a crash
mRequest->SetHeader("Content-Type", "application/json");
mRequest->SetURL(mUrl);
mRequest->OnProcessRequestComplete().BindLambda([this, startMs, mRequest](FHttpRequestPtr requestPtr, FHttpResponsePtr responsePtr, bool success) {
FString ResponseString = "";
if (success)
{
ResponseString = responsePtr->GetContentAsString();
}
CompletedCallback.Broadcast(ResponseString, success);
});
bool result = mRequest->ProcessRequest();
}
the crash happens inside MTAccessDetector.h
in this line
ensureMsgf(ReaderIndex != INDEX_NONE,
TEXT("Either a race detected (%u reader(s) on another thread(s) while acquiring write access on the current thread) or the access detector was trivially relocated:\nCurrent thread %u callstack:\n%s"),
LocalState.ReaderNum, FPlatformTLS::GetCurrentThreadId(), *GetCurrentThreadCallstack());
is there a way to know if the CompletedCallback is still valid?