From 5383589f991c0b79116bb8dd59c64f435eb66d95 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 17 Sep 2023 20:01:43 -0700 Subject: [PATCH] [wpinet] uv::Request: Return shared_ptr from Release() (#5656) Use this in Stream to allow the finish() callback to reuse the request. --- wpinet/src/main/native/cpp/uv/Stream.cpp | 2 +- wpinet/src/main/native/include/wpinet/uv/Request.h | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/wpinet/src/main/native/cpp/uv/Stream.cpp b/wpinet/src/main/native/cpp/uv/Stream.cpp index fdceb7b1f2..83753cb2a4 100644 --- a/wpinet/src/main/native/cpp/uv/Stream.cpp +++ b/wpinet/src/main/native/cpp/uv/Stream.cpp @@ -97,8 +97,8 @@ void Stream::Write(std::span bufs, if (status < 0) { h.ReportError(status); } + auto ptr = h.Release(); // one-shot, but finish() may Keep() h.finish(Error(status)); - h.Release(); // this is always a one-shot })) { req->Keep(); } diff --git a/wpinet/src/main/native/include/wpinet/uv/Request.h b/wpinet/src/main/native/include/wpinet/uv/Request.h index fe1f97cf3a..d16d289dce 100644 --- a/wpinet/src/main/native/include/wpinet/uv/Request.h +++ b/wpinet/src/main/native/include/wpinet/uv/Request.h @@ -9,6 +9,7 @@ #include #include +#include #include "wpinet/uv/Error.h" @@ -92,8 +93,12 @@ class Request : public std::enable_shared_from_this { * * Derived classes can override this method for different memory management * approaches (e.g. pooled storage of requests). + * + * @return Previous shared pointer */ - virtual void Release() noexcept { m_self.reset(); } + virtual std::shared_ptr Release() noexcept { + return std::move(m_self); + } /** * Error callback. By default, this is set up to report errors to the handle