@@ -47,14 +47,20 @@ void ULocalhostListener::WaitForResponse(TSuccessCallback<FString> OnSuccess, FF
4747 return ;
4848 }
4949
50+ TWeakObjectPtr<ULocalhostListener> WeakThis (this );
5051
5152 const FHttpPath RootPath (TEXT (" /api" ));
5253 RouteHandle = Router->BindRoute (
5354 RootPath,
5455 EHttpServerRequestVerbs::VERB_GET,
55- FHttpRequestHandler::CreateLambda ([this ](const FHttpServerRequest& Request, const FHttpResultCallback& OnComplete) -> bool
56+ FHttpRequestHandler::CreateLambda ([WeakThis ](const FHttpServerRequest& Request, const FHttpResultCallback& OnComplete) -> bool
5657 {
57- return this ->HandleAnyRequest (Request, OnComplete);
58+ if (!WeakThis.IsValid ())
59+ {
60+ return false ;
61+ }
62+
63+ return WeakThis->HandleAnyRequest (Request, OnComplete);
5864 })
5965 );
6066
@@ -85,29 +91,42 @@ bool ULocalhostListener::HandleAnyRequest(const FHttpServerRequest& Request, con
8591
8692 if (Request.QueryParams .Contains (" error" ))
8793 {
88- if (CurrentOnFailure.IsValid () && *CurrentOnFailure)
89- {
90- (*CurrentOnFailure)(FSequenceError (EErrorType::InvalidArgument, Request.QueryParams [" error" ]));
91- }
92- else
93- {
94- UE_LOG (LogTemp, Error, TEXT (" CurrentOnFailure is invalid" ));
95- }
94+ if (TSharedPtr<FFailureCallback> Failure = CurrentOnFailure.Pin ())
95+ {
96+ if (*Failure)
97+ {
98+ const FString* Error = Request.QueryParams .Find (" error" );
99+ (*Failure)(FSequenceError (EErrorType::InvalidArgument, *Error));
100+ }
101+ }
102+ else
103+ {
104+ UE_LOG (LogTemp, Warning, TEXT (" Failure callback expired" ));
105+ }
96106 }
97107 else
98108 {
99- const FString EncodedPayload = Request.QueryParams [" payload" ];
109+ const FString* EncodedPayload = Request.QueryParams .Find (" payload" );
110+ if (!EncodedPayload)
111+ {
112+ UE_LOG (LogTemp, Warning, TEXT (" Missing payload param" ));
113+ return false ;
114+ }
115+
100116 const FString PayloadJson = USequenceSupport::DecodeBase64ToString (EncodedPayload);
101117
102118 UE_LOG (LogTemp, Log, TEXT (" Response Payload Json %s" ), *PayloadJson);
103119
104- if (CurrentOnSuccess.IsValid () && *CurrentOnSuccess )
120+ if (TSharedPtr<TSuccessCallback<FString>> Success = CurrentOnSuccess.Pin () )
105121 {
106- (*CurrentOnSuccess)(PayloadJson);
122+ if (*Success)
123+ {
124+ (*Success)(PayloadJson);
125+ }
107126 }
108127 else
109128 {
110- UE_LOG (LogTemp, Error , TEXT (" CurrentOnSuccess is invalid " ));
129+ UE_LOG (LogTemp, Warning , TEXT (" Success callback expired " ));
111130 }
112131 }
113132
0 commit comments