From 8a753e5a2a8eecd50e6f52593013d3fb8e68711b Mon Sep 17 00:00:00 2001 From: Michael Wilkerson-Barker Date: Tue, 27 Aug 2024 17:36:22 -0400 Subject: [PATCH] Updates from review --- src/realm/object-store/sync/app.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/realm/object-store/sync/app.cpp b/src/realm/object-store/sync/app.cpp index e66e0ee208..f1b7350fed 100644 --- a/src/realm/object-store/sync/app.cpp +++ b/src/realm/object-store/sync/app.cpp @@ -1176,6 +1176,11 @@ void App::post(std::string&& route, UniqueFunction)>&& c void App::do_request(std::unique_ptr&& request, IntermediateCompletion&& completion, bool update_location) { + // NOTE: Since the calls to `send_request_to_server()` or `upldate_location_and_resend()` do not + // capture a shared_ptr to App as part of their callback, any function that calls `do_request()` + // needs to capture the App as `self = shared_from_this()` to ensure the lifetime of the App + // object is extended until the callback is called after the operation is complete. + // Verify the request URL to make sure it is valid if (auto valid_url = util::Uri::try_parse(request->url); !valid_url.is_ok()) { completion(std::move(request), AppUtils::make_apperror_response( @@ -1258,10 +1263,10 @@ void App::handle_auth_failure(const AppError& error, std::unique_ptr&& // Reissue the request with the new access token request->headers = get_request_headers(user, RequestTokenType::AccessToken); - self->do_request(std::move(request), - [completion = std::move(completion)](auto&&, auto& response) { - completion(response); - }); + self->do_request(std::move(request), [self = self, completion = std::move(completion)]( + auto&&, auto& response) { + completion(response); + }); }); }