diff --git a/src/cbs.cpp b/src/cbs.cpp index d933d870a..9d98da561 100644 --- a/src/cbs.cpp +++ b/src/cbs.cpp @@ -32,7 +32,7 @@ namespace cbs { /** * @brief Owning coded bitstream context pointer that calls ff_cbs_close. */ - using ctx_t = util::safe_ptr; + using ctx_t = util::safe_ptr; /** * @brief Owns an FFmpeg coded bitstream fragment and frees fragment buffers on destruction. diff --git a/src/crypto.cpp b/src/crypto.cpp index 5e39122de..ca8d4de40 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -13,7 +13,7 @@ namespace crypto { /** * @brief OpenSSL ASN.1 string pointer with automatic release. */ - using asn1_string_t = util::safe_ptr; + using asn1_string_t = util::safe_ptr; /** * @brief OpenSSL X.509 subject/issuer name pointer with automatic release. */ diff --git a/src/crypto.h b/src/crypto.h index 6622c17d8..1a1c00f04 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -44,39 +44,39 @@ namespace crypto { /** * @brief Owning pointer for an OpenSSL X.509 certificate. */ - using x509_t = util::safe_ptr; + using x509_t = util::safe_ptr; /** * @brief Owning pointer for an OpenSSL certificate store. */ - using x509_store_t = util::safe_ptr; + using x509_store_t = util::safe_ptr; /** * @brief Owning pointer for an OpenSSL certificate verification context. */ - using x509_store_ctx_t = util::safe_ptr; + using x509_store_ctx_t = util::safe_ptr; /** * @brief Owning pointer for an OpenSSL cipher context. */ - using cipher_ctx_t = util::safe_ptr; + using cipher_ctx_t = util::safe_ptr; /** * @brief Owning pointer for an OpenSSL message digest context. */ - using md_ctx_t = util::safe_ptr; + using md_ctx_t = util::safe_ptr; /** * @brief Owning pointer for an OpenSSL BIO chain. */ - using bio_t = util::safe_ptr; + using bio_t = util::safe_ptr; /** * @brief Owning pointer for an OpenSSL public or private key. */ - using pkey_t = util::safe_ptr; + using pkey_t = util::safe_ptr; /** * @brief Owning pointer for an OpenSSL key-generation context. */ - using pkey_ctx_t = util::safe_ptr; + using pkey_ctx_t = util::safe_ptr; /** * @brief Owning pointer for an OpenSSL BIGNUM. */ - using bignum_t = util::safe_ptr; + using bignum_t = util::safe_ptr; /** * @brief Hashes the given plaintext using SHA-256. diff --git a/src/network.h b/src/network.h index 137dcdef8..f682fd351 100644 --- a/src/network.h +++ b/src/network.h @@ -37,7 +37,7 @@ namespace net { /** * @brief Owning ENet host pointer released with `enet_host_destroy`. */ - using host_t = util::safe_ptr; + using host_t = util::safe_ptr; /** * @brief Raw ENet peer handle owned by an ENet host. */ @@ -45,7 +45,7 @@ namespace net { /** * @brief Owning ENet packet pointer released with `enet_packet_destroy`. */ - using packet_t = util::safe_ptr; + using packet_t = util::safe_ptr; /** * @brief Enumerates supported net options. diff --git a/src/platform/linux/audio.cpp b/src/platform/linux/audio.cpp index 43db87b10..f7439eb61 100644 --- a/src/platform/linux/audio.cpp +++ b/src/platform/linux/audio.cpp @@ -67,7 +67,7 @@ namespace platf { * @brief PulseAudio recording stream and channel metadata. */ struct mic_attr_t: public mic_t { - util::safe_ptr mic; ///< PulseAudio simple recording stream for microphone capture. + util::safe_ptr mic; ///< PulseAudio simple recording stream for microphone capture. /** * @brief Deliver a captured audio sample to Sunshine's audio pipeline. @@ -218,19 +218,19 @@ namespace platf { /** * @brief Owning pointer for a PulseAudio context. */ - using ctx_t = util::safe_ptr; + using ctx_t = util::safe_ptr; /** * @brief Owning pointer for a PulseAudio mainloop. */ - using loop_t = util::safe_ptr; + using loop_t = util::safe_ptr; /** * @brief Owning pointer for a PulseAudio asynchronous operation. */ - using op_t = util::safe_ptr; + using op_t = util::safe_ptr; /** * @brief Owning pointer for PulseAudio strings allocated with `pa_xmalloc`. */ - using string_t = util::safe_ptr>; + using string_t = util::safe_ptr>; /** * @brief Callback wrapper for PulseAudio introspection results without an end marker. diff --git a/src/platform/linux/graphics.h b/src/platform/linux/graphics.h index fbcdc728a..c10391ca8 100644 --- a/src/platform/linux/graphics.h +++ b/src/platform/linux/graphics.h @@ -63,7 +63,7 @@ void free_frame(AVFrame *frame); /** * @brief Owning pointer for an EGL frame object. */ -using frame_t = util::safe_ptr; +using frame_t = util::safe_ptr; namespace gl { extern GladGLContext ctx; diff --git a/src/platform/linux/kmsgrab.cpp b/src/platform/linux/kmsgrab.cpp index 988f5c964..29168579a 100644 --- a/src/platform/linux/kmsgrab.cpp +++ b/src/platform/linux/kmsgrab.cpp @@ -300,19 +300,19 @@ namespace platf { /** * @brief DRM plane resource list released with `drmModeFreePlaneResources`. */ - using plane_res_t = util::safe_ptr; + using plane_res_t = util::safe_ptr; /** * @brief DRM encoder pointer released with `drmModeFreeEncoder`. */ - using encoder_t = util::safe_ptr; + using encoder_t = util::safe_ptr; /** * @brief DRM resource list released with `drmModeFreeResources`. */ - using res_t = util::safe_ptr; + using res_t = util::safe_ptr; /** * @brief DRM plane pointer released with `drmModeFreePlane`. */ - using plane_t = util::safe_ptr; + using plane_t = util::safe_ptr; /** * @brief DRM framebuffer pointer released with `drmModeFreeFB2`. */ @@ -320,23 +320,23 @@ namespace platf { /** * @brief DRM CRTC pointer released with `drmModeFreeCrtc`. */ - using crtc_t = util::safe_ptr; + using crtc_t = util::safe_ptr; /** * @brief DRM object-property list released with `drmModeFreeObjectProperties`. */ - using obj_prop_t = util::safe_ptr; + using obj_prop_t = util::safe_ptr; /** * @brief DRM property pointer released with `drmModeFreeProperty`. */ - using prop_t = util::safe_ptr; + using prop_t = util::safe_ptr; /** * @brief DRM property-blob pointer released with `drmModeFreePropertyBlob`. */ - using prop_blob_t = util::safe_ptr; + using prop_blob_t = util::safe_ptr; /** * @brief DRM version pointer released with `drmFreeVersion`. */ - using version_t = util::safe_ptr; + using version_t = util::safe_ptr; /** * @brief Counter map keyed by DRM connector type. @@ -689,7 +689,7 @@ namespace platf { /** * @brief Internal connector metadata tuple used while selecting a monitor. */ - using connector_interal_t = util::safe_ptr; + using connector_interal_t = util::safe_ptr; /** * @brief Open a DRM connector and cache its monitor metadata. diff --git a/src/platform/linux/misc.cpp b/src/platform/linux/misc.cpp index babf0a488..0d49aa872 100644 --- a/src/platform/linux/misc.cpp +++ b/src/platform/linux/misc.cpp @@ -200,7 +200,7 @@ namespace platf { /** * @brief Owning pointer for `getifaddrs` results. */ - using ifaddr_t = util::safe_ptr; + using ifaddr_t = util::safe_ptr; /** * @brief Open a DRM card node, dropping implicit DRM master when possible. diff --git a/src/platform/linux/vaapi.cpp b/src/platform/linux/vaapi.cpp index 1badbb7a8..20942e4a1 100644 --- a/src/platform/linux/vaapi.cpp +++ b/src/platform/linux/vaapi.cpp @@ -154,7 +154,7 @@ namespace va { /** * @brief VA display handle released with `vaTerminate`. */ - using display_t = util::safe_ptr_v2; + using display_t = util::safe_ptr_v2; /** * @brief Create an FFmpeg VA-API hardware device context from a Sunshine encode device. diff --git a/src/platform/linux/x11grab.cpp b/src/platform/linux/x11grab.cpp index a14b3b66d..c9c98f571 100644 --- a/src/platform/linux/x11grab.cpp +++ b/src/platform/linux/x11grab.cpp @@ -296,11 +296,11 @@ namespace platf { /** * @brief XImage pointer released with `XDestroyImage`. */ - using ximg_t = util::safe_ptr; + using ximg_t = util::safe_ptr; /** * @brief XFixes cursor image pointer released with `XFree`. */ - using xcursor_t = util::safe_ptr; + using xcursor_t = util::safe_ptr; /** * @brief XRandR CRTC info pointer released with `XRRFreeCrtcInfo`. diff --git a/src/platform/linux/x11grab.h b/src/platform/linux/x11grab.h index fd53c0fc2..a48c28af4 100644 --- a/src/platform/linux/x11grab.h +++ b/src/platform/linux/x11grab.h @@ -36,11 +36,11 @@ namespace platf::x11 { /** * @brief XFixes cursor image pointer released with `XFree`. */ - using cursor_ctx_t = util::safe_ptr; + using cursor_ctx_t = util::safe_ptr; /** * @brief X11 display pointer released with `XCloseDisplay`. */ - using xdisplay_t = util::safe_ptr<_XDisplay, freeDisplay>; + using xdisplay_t = util::safe_ptr<_XDisplay, &freeDisplay>; /** * @brief X11 cursor image and positioning state used during capture. diff --git a/src/platform/macos/nv12_zero_device.cpp b/src/platform/macos/nv12_zero_device.cpp index 977019879..dc1be63f3 100644 --- a/src/platform/macos/nv12_zero_device.cpp +++ b/src/platform/macos/nv12_zero_device.cpp @@ -33,7 +33,7 @@ namespace platf { CVPixelBufferRelease((CVPixelBufferRef) data); } - util::safe_ptr av_frame; ///< AV frame. + util::safe_ptr av_frame; ///< AV frame. int nv12_zero_device::convert(platf::img_t &img) { auto *av_img = (av_img_t *) &img; diff --git a/src/platform/macos/nv12_zero_device.h b/src/platform/macos/nv12_zero_device.h index bd92b025b..7b6d68faa 100644 --- a/src/platform/macos/nv12_zero_device.h +++ b/src/platform/macos/nv12_zero_device.h @@ -66,7 +66,7 @@ namespace platf { int set_frame(AVFrame *frame, AVBufferRef *hw_frames_ctx) override; private: - util::safe_ptr av_frame; + util::safe_ptr av_frame; }; } // namespace platf diff --git a/src/platform/windows/audio.cpp b/src/platform/windows/audio.cpp index e00033f33..d12f983a8 100644 --- a/src/platform/windows/audio.cpp +++ b/src/platform/windows/audio.cpp @@ -246,43 +246,43 @@ namespace platf::audio { /** * @brief COM device enumerator pointer for WASAPI endpoint discovery. */ - using device_enum_t = util::safe_ptr>; + using device_enum_t = util::safe_ptr>; /** * @brief COM pointer to a Windows audio endpoint device. */ - using device_t = util::safe_ptr>; + using device_t = util::safe_ptr>; /** * @brief COM pointer to a collection of Windows audio endpoint devices. */ - using collection_t = util::safe_ptr>; + using collection_t = util::safe_ptr>; /** * @brief COM pointer to the WASAPI audio client interface. */ - using audio_client_t = util::safe_ptr>; + using audio_client_t = util::safe_ptr>; /** * @brief COM pointer to the WASAPI capture client interface. */ - using audio_capture_t = util::safe_ptr>; + using audio_capture_t = util::safe_ptr>; /** * @brief CoTaskMem-allocated WAVEFORMATEX pointer. */ - using wave_format_t = util::safe_ptr>; + using wave_format_t = util::safe_ptr>; /** * @brief CoTaskMem-allocated wide string pointer. */ - using wstring_t = util::safe_ptr>; + using wstring_t = util::safe_ptr>; /** * @brief Windows HANDLE wrapper closed with `CloseHandle`. */ - using handle_t = util::safe_ptr_v2; + using handle_t = util::safe_ptr_v2; /** * @brief COM pointer to the Windows policy configuration interface. */ - using policy_t = util::safe_ptr>; + using policy_t = util::safe_ptr>; /** * @brief COM pointer to a Windows property store. */ - using prop_t = util::safe_ptr>; + using prop_t = util::safe_ptr>; /** * @brief Initializes COM for the current thread and uninitializes it on exit. diff --git a/src/platform/windows/display.h b/src/platform/windows/display.h index cfd588ded..af10f1206 100644 --- a/src/platform/windows/display.h +++ b/src/platform/windows/display.h @@ -42,149 +42,149 @@ namespace platf::dxgi { /** * @brief Owning COM pointer for the DXGI factory used to enumerate adapters. */ - using factory1_t = util::safe_ptr>; + using factory1_t = util::safe_ptr>; /** * @brief Owning COM pointer for a DXGI device interface. */ - using dxgi_t = util::safe_ptr>; + using dxgi_t = util::safe_ptr>; /** * @brief Owning COM pointer for a DXGI 1.1 device interface. */ - using dxgi1_t = util::safe_ptr>; + using dxgi1_t = util::safe_ptr>; /** * @brief Owning COM pointer for the D3D11 device used by capture and conversion. */ - using device_t = util::safe_ptr>; + using device_t = util::safe_ptr>; /** * @brief Owning COM pointer for the D3D11.1 device interface. */ - using device1_t = util::safe_ptr>; + using device1_t = util::safe_ptr>; /** * @brief Owning COM pointer for the immediate D3D11 device context. */ - using device_ctx_t = util::safe_ptr>; + using device_ctx_t = util::safe_ptr>; /** * @brief Owning COM pointer for a DXGI display adapter. */ - using adapter_t = util::safe_ptr>; + using adapter_t = util::safe_ptr>; /** * @brief Owning COM pointer for a DXGI output. */ - using output_t = util::safe_ptr>; + using output_t = util::safe_ptr>; /** * @brief Owning COM pointer for a DXGI output that supports duplication. */ - using output1_t = util::safe_ptr>; + using output1_t = util::safe_ptr>; /** * @brief Owning COM pointer for a DXGI output with HDR metadata support. */ - using output5_t = util::safe_ptr>; + using output5_t = util::safe_ptr>; /** * @brief Owning COM pointer for a DXGI output with modern display descriptors. */ - using output6_t = util::safe_ptr>; + using output6_t = util::safe_ptr>; /** * @brief Owning COM pointer for the DXGI desktop duplication session. */ - using dup_t = util::safe_ptr>; + using dup_t = util::safe_ptr>; /** * @brief Owning COM pointer for a D3D11 2D texture. */ - using texture2d_t = util::safe_ptr>; + using texture2d_t = util::safe_ptr>; /** * @brief Owning COM pointer for a D3D11 1D texture. */ - using texture1d_t = util::safe_ptr>; + using texture1d_t = util::safe_ptr>; /** * @brief Owning COM pointer for the DXGI resource view of a shared texture. */ - using resource_t = util::safe_ptr>; + using resource_t = util::safe_ptr>; /** * @brief Owning COM pointer for the DXGI 1.1 resource sharing interface. */ - using resource1_t = util::safe_ptr>; + using resource1_t = util::safe_ptr>; /** * @brief Owning COM pointer for the D3D11 multithread-protection interface. */ - using multithread_t = util::safe_ptr>; + using multithread_t = util::safe_ptr>; /** * @brief Owning COM pointer for a D3D11 vertex shader. */ - using vs_t = util::safe_ptr>; + using vs_t = util::safe_ptr>; /** * @brief Owning COM pointer for a D3D11 pixel shader. */ - using ps_t = util::safe_ptr>; + using ps_t = util::safe_ptr>; /** * @brief Owning COM pointer for a D3D11 blend state. */ - using blend_t = util::safe_ptr>; + using blend_t = util::safe_ptr>; /** * @brief Owning COM pointer for a D3D11 input layout. */ - using input_layout_t = util::safe_ptr>; + using input_layout_t = util::safe_ptr>; /** * @brief Owning COM pointer for a D3D11 render-target view. */ - using render_target_t = util::safe_ptr>; + using render_target_t = util::safe_ptr>; /** * @brief Owning COM pointer for a D3D11 shader-resource view. */ - using shader_res_t = util::safe_ptr>; + using shader_res_t = util::safe_ptr>; /** * @brief Owning COM pointer for a D3D11 buffer. */ - using buf_t = util::safe_ptr>; + using buf_t = util::safe_ptr>; /** * @brief D3D rasterizer state handle type. */ - using raster_state_t = util::safe_ptr>; + using raster_state_t = util::safe_ptr>; /** * @brief D3D sampler state handle type. */ - using sampler_state_t = util::safe_ptr>; + using sampler_state_t = util::safe_ptr>; /** * @brief Owning COM pointer for a compiled shader bytecode blob. */ - using blob_t = util::safe_ptr>; + using blob_t = util::safe_ptr>; /** * @brief D3D depth-stencil state handle type. */ - using depth_stencil_state_t = util::safe_ptr>; + using depth_stencil_state_t = util::safe_ptr>; /** * @brief Owning COM pointer for a D3D11 depth-stencil view. */ - using depth_stencil_view_t = util::safe_ptr>; + using depth_stencil_view_t = util::safe_ptr>; /** * @brief Owning COM pointer for a keyed mutex on a shared DXGI resource. */ - using keyed_mutex_t = util::safe_ptr>; + using keyed_mutex_t = util::safe_ptr>; namespace video { /** * @brief Owning COM pointer for the D3D11 video device interface. */ - using device_t = util::safe_ptr>; + using device_t = util::safe_ptr>; /** * @brief Owning COM pointer for the D3D11 video context interface. */ - using ctx_t = util::safe_ptr>; + using ctx_t = util::safe_ptr>; /** * @brief Owning COM pointer for a D3D11 video processor. */ - using processor_t = util::safe_ptr>; + using processor_t = util::safe_ptr>; /** * @brief Owning COM pointer for a video processor output view. */ - using processor_out_t = util::safe_ptr>; + using processor_out_t = util::safe_ptr>; /** * @brief Owning COM pointer for a video processor input view. */ - using processor_in_t = util::safe_ptr>; + using processor_in_t = util::safe_ptr>; /** * @brief Owning COM pointer for a video processor capability enumerator. */ - using processor_enum_t = util::safe_ptr>; + using processor_enum_t = util::safe_ptr>; } // namespace video /** diff --git a/src/platform/windows/display_vram.cpp b/src/platform/windows/display_vram.cpp index b7b3c743a..d4b7141e2 100644 --- a/src/platform/windows/display_vram.cpp +++ b/src/platform/windows/display_vram.cpp @@ -47,7 +47,7 @@ static void free_frame(AVFrame *frame) { /** * @brief FFmpeg hardware frame pointer released with `av_frame_free`. */ -using frame_t = util::safe_ptr; +using frame_t = util::safe_ptr; namespace platf::dxgi { diff --git a/src/platform/windows/input.cpp b/src/platform/windows/input.cpp index 2a2176d5e..08f10e690 100644 --- a/src/platform/windows/input.cpp +++ b/src/platform/windows/input.cpp @@ -40,11 +40,11 @@ namespace platf { /** * @brief ViGEm client pointer released with `vigem_free`. */ - using client_t = util::safe_ptr<_VIGEM_CLIENT_T, vigem_free>; + using client_t = util::safe_ptr<_VIGEM_CLIENT_T, &vigem_free>; /** * @brief ViGEm target pointer released with `vigem_target_free`. */ - using target_t = util::safe_ptr<_VIGEM_TARGET_T, vigem_target_free>; + using target_t = util::safe_ptr<_VIGEM_TARGET_T, &vigem_target_free>; /** * @brief Handle Xbox 360 virtual gamepad notification events. diff --git a/src/platform/windows/nvprefs/nvprefs_common.h b/src/platform/windows/nvprefs/nvprefs_common.h index f422f159b..3ba41398b 100644 --- a/src/platform/windows/nvprefs/nvprefs_common.h +++ b/src/platform/windows/nvprefs/nvprefs_common.h @@ -19,8 +19,8 @@ namespace nvprefs { /** * @brief Owning Windows HANDLE wrapper that closes handles automatically. */ - struct safe_handle: public util::safe_ptr_v2 { - using util::safe_ptr_v2::safe_ptr_v2; + struct safe_handle: public util::safe_ptr_v2 { + using util::safe_ptr_v2::safe_ptr_v2; /** * @brief Check whether the wrapped Windows handle is valid. @@ -54,7 +54,7 @@ namespace nvprefs { /** * @brief Safe pointer for Windows security identifiers released with `FreeSid`. */ - using safe_sid = util::safe_ptr_v2; + using safe_sid = util::safe_ptr_v2; /** * @brief Forward an informational NVPrefs message to the logger. diff --git a/src/process.h b/src/process.h index 26bb470a1..613f1fe29 100644 --- a/src/process.h +++ b/src/process.h @@ -33,7 +33,7 @@ namespace proc { /** * @brief Boost.Process pipe stream used for child-process I/O. */ - using file_t = util::safe_ptr_v2; + using file_t = util::safe_ptr_v2; /** * @brief Parsed command arguments used when launching a child process. diff --git a/src/rtsp.cpp b/src/rtsp.cpp index 481300d9a..554d6d3fa 100644 --- a/src/rtsp.cpp +++ b/src/rtsp.cpp @@ -108,7 +108,7 @@ namespace rtsp_stream { /** * @brief Owning pointer for an RTSP message allocated by moonlight-common-c. */ - using msg_t = util::safe_ptr; + using msg_t = util::safe_ptr; /** * @brief RTSP command handler signature used by the server dispatch table. */ diff --git a/src/video.h b/src/video.h index 63f3cf55c..412b40c27 100644 --- a/src/video.h +++ b/src/video.h @@ -99,21 +99,21 @@ namespace video { void free_buffer(AVBufferRef *ref); /** - * @brief Owning pointer for an FFmpeg codec context. + * @brief Owning pointer for an FFmpeg codec context released with free_ctx(). */ - using avcodec_ctx_t = util::safe_ptr; + using avcodec_ctx_t = util::safe_ptr; /** - * @brief Owning pointer for an FFmpeg frame. + * @brief Owning pointer for an FFmpeg frame released with free_frame(). */ - using avcodec_frame_t = util::safe_ptr; + using avcodec_frame_t = util::safe_ptr; /** - * @brief Owning pointer for an FFmpeg buffer reference. + * @brief Owning pointer for an FFmpeg buffer reference released with free_buffer(). */ - using avcodec_buffer_t = util::safe_ptr; + using avcodec_buffer_t = util::safe_ptr; /** - * @brief Owning pointer for an FFmpeg software-scaling context. + * @brief Owning pointer for an FFmpeg software-scaling context released with sws_freeContext(). */ - using sws_t = util::safe_ptr; + using sws_t = util::safe_ptr; /** * @brief Shared event that transports captured images between capture and encode threads. */ diff --git a/tests/unit/test_resource_ownership.cpp b/tests/unit/test_resource_ownership.cpp new file mode 100644 index 000000000..5d62d2bf5 --- /dev/null +++ b/tests/unit/test_resource_ownership.cpp @@ -0,0 +1,184 @@ +/** + * @file tests/unit/test_resource_ownership.cpp + * @brief Tests for resource lifetime through Sunshine's owning pointer aliases. + */ + +// test includes +#include "../tests_common.h" + +// standard includes +#include +#include +#include +#include + +// platform includes +#ifdef _WIN32 + // Winsock must precede Windows.h. + // clang-format off + #include + #include + // clang-format on + #include +#else + #include + #include +#endif + +// local includes +#include +#include +#include +#ifdef _WIN32 + #include +#endif + +namespace { + /** + * @brief Allocate an FFmpeg buffer that records its final release. + * + * @param releases Counter incremented when the buffer has no remaining references. + * @param data Storage kept alive until the buffer is released. + * @return Owning reference to the tracked buffer. + */ + video::avcodec_buffer_t tracked_buffer(int &releases, uint8_t &data) { + return video::avcodec_buffer_t {av_buffer_create(&data, 1, [](auto *opaque, uint8_t *) { + ++*static_cast(opaque); + }, + &releases, + 0)}; + } +} // namespace + +/** + * @brief Moving and resetting a frame releases its attached buffer exactly once. + */ +TEST(ResourceOwnershipTest, FrameMoveTransfersBufferOwnership) { + int releases = 0; + uint8_t data = 0; + auto buffer = tracked_buffer(releases, data); + ASSERT_TRUE(buffer); + video::avcodec_frame_t frame {av_frame_alloc()}; + ASSERT_TRUE(frame); + frame->buf[0] = buffer.release(); + + auto moved = std::move(frame); + EXPECT_FALSE(frame); + EXPECT_EQ(releases, 0); + moved.reset(); + EXPECT_EQ(releases, 1); + moved.reset(); + EXPECT_EQ(releases, 1); +} + +/** + * @brief Destroying a codec context releases its hardware-frame context reference. + */ +TEST(ResourceOwnershipTest, CodecContextDestructionReleasesHardwareFramesReference) { + int releases = 0; + uint8_t data = 0; + { + auto buffer = tracked_buffer(releases, data); + ASSERT_TRUE(buffer); + video::avcodec_ctx_t context {avcodec_alloc_context3(nullptr)}; + ASSERT_TRUE(context); + context->hw_frames_ctx = buffer.release(); + EXPECT_EQ(releases, 0); + } + EXPECT_EQ(releases, 1); +} + +/** + * @brief An FFmpeg buffer remains alive until its final owning reference is destroyed. + */ +TEST(ResourceOwnershipTest, BufferDestructionPreservesOtherReferences) { + int releases = 0; + uint8_t data = 0; + { + auto first = tracked_buffer(releases, data); + ASSERT_TRUE(first); + video::avcodec_buffer_t second {av_buffer_ref(first.get())}; + ASSERT_TRUE(second); + first.reset(); + EXPECT_EQ(releases, 0); + EXPECT_EQ(av_buffer_get_ref_count(second.get()), 1); + } + EXPECT_EQ(releases, 1); +} + +/** + * @brief Resetting a packet owner destroys each displaced ENet packet exactly once. + */ +TEST(ResourceOwnershipTest, PacketResetReleasesReplacedPacket) { + int releases = 0; + const auto make_packet = [&releases]() { + net::packet_t packet {enet_packet_create(nullptr, 0, 0)}; + if (packet) { + packet->userData = &releases; + packet->freeCallback = [](ENetPacket *released) { + ++*static_cast(released->userData); + }; + } + return packet; + }; + + { + auto packet = make_packet(); + auto replacement = make_packet(); + ASSERT_TRUE(packet); + ASSERT_TRUE(replacement); + packet.reset(replacement.release()); + EXPECT_EQ(releases, 1); + } + EXPECT_EQ(releases, 2); +} + +/** + * @brief Resetting a file owner closes a securely created temporary file's underlying resource. + */ +TEST(ResourceOwnershipTest, FileResetClosesUnderlyingDescriptor) { +#ifdef _WIN32 + FILE *temporary_file = nullptr; + ASSERT_EQ(tmpfile_s(&temporary_file), 0); + proc::file_t file {temporary_file}; +#else + proc::file_t file {std::tmpfile()}; +#endif + ASSERT_TRUE(file); +#ifdef _WIN32 + const auto handle = std::bit_cast(_get_osfhandle(_fileno(file.get()))); + ASSERT_NE(handle, INVALID_HANDLE_VALUE); + file.reset(); + DWORD flags = 0; + EXPECT_FALSE(GetHandleInformation(handle, &flags)); + EXPECT_EQ(GetLastError(), ERROR_INVALID_HANDLE); +#else + const auto descriptor = fileno(file.get()); + ASSERT_GE(descriptor, 0); + file.reset(); + errno = 0; + EXPECT_EQ(fcntl(descriptor, F_GETFD), -1); + EXPECT_EQ(errno, EBADF); +#endif +} + +#ifdef _WIN32 +/** + * @brief Moving an NVPrefs handle transfers responsibility for closing it. + */ +TEST(ResourceOwnershipTest, WindowsHandleMoveClosesHandleOnDestruction) { + HANDLE raw_handle = nullptr; + { + nvprefs::safe_handle handle {CreateEventW(nullptr, TRUE, FALSE, nullptr)}; + ASSERT_TRUE(handle); + raw_handle = handle.get(); + auto moved = std::move(handle); + EXPECT_FALSE(handle); + EXPECT_TRUE(moved); + EXPECT_EQ(WaitForSingleObject(raw_handle, 0), WAIT_TIMEOUT); + } + DWORD flags = 0; + EXPECT_FALSE(GetHandleInformation(raw_handle, &flags)); + EXPECT_EQ(GetLastError(), ERROR_INVALID_HANDLE); +} +#endif diff --git a/tools/audio.cpp b/tools/audio.cpp index 0b8ca3b65..dbe96b4b8 100644 --- a/tools/audio.cpp +++ b/tools/audio.cpp @@ -35,17 +35,17 @@ namespace audio { CoTaskMemFree(static_cast(p)); } - using device_enum_t = util::safe_ptr>; - using collection_t = util::safe_ptr>; - using prop_t = util::safe_ptr>; - using device_t = util::safe_ptr>; - using audio_client_t = util::safe_ptr>; - using audio_capture_t = util::safe_ptr>; - using wave_format_t = util::safe_ptr>; + using device_enum_t = util::safe_ptr>; + using collection_t = util::safe_ptr>; + using prop_t = util::safe_ptr>; + using device_t = util::safe_ptr>; + using audio_client_t = util::safe_ptr>; + using audio_capture_t = util::safe_ptr>; + using wave_format_t = util::safe_ptr>; - using wstring_t = util::safe_ptr>; + using wstring_t = util::safe_ptr>; - using handle_t = util::safe_ptr_v2; + using handle_t = util::safe_ptr_v2; class prop_var_t { public: diff --git a/tools/dxgi.cpp b/tools/dxgi.cpp index f1f63e1c1..a52e656bc 100644 --- a/tools/dxgi.cpp +++ b/tools/dxgi.cpp @@ -19,9 +19,9 @@ namespace dxgi { dxgi->Release(); } - using factory1_t = util::safe_ptr>; - using adapter_t = util::safe_ptr>; - using output_t = util::safe_ptr>; + using factory1_t = util::safe_ptr>; + using adapter_t = util::safe_ptr>; + using output_t = util::safe_ptr>; } // namespace dxgi int main(int argc, char *argv[]) {