From 6aeaeed3f017a499b9ce354cfaf6fecb601fff78 Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Thu, 19 Feb 2026 14:27:13 +0100 Subject: [PATCH 1/4] Replace RGBImagePtr by RGBImageCleanup --- include/avif/avif_cxx.h | 14 +++++++++++--- tests/gtest/avifbasictest.cc | 12 ++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/avif/avif_cxx.h b/include/avif/avif_cxx.h index f369978cba..fc4cfd7d87 100644 --- a/include/avif/avif_cxx.h +++ b/include/avif/avif_cxx.h @@ -22,7 +22,6 @@ struct UniquePtrDeleter void operator()(avifEncoder * encoder) const { avifEncoderDestroy(encoder); } void operator()(avifGainMap * gainMap) const { avifGainMapDestroy(gainMap); } void operator()(avifImage * image) const { avifImageDestroy(image); } - void operator()(avifRGBImage * image) const { avifRGBImageFreePixels(image); } }; // Use these unique_ptr to ensure the structs are automatically destroyed. @@ -30,8 +29,17 @@ using DecoderPtr = std::unique_ptr; using EncoderPtr = std::unique_ptr; using GainMapPtr = std::unique_ptr; using ImagePtr = std::unique_ptr; -// To use when RGBImage actually owns the pixels. RGBImage can also be used as a view, in which case it does not own the pixels. -using RGBImagePtr = std::unique_ptr; + +// Automatically cleans the ressources of the avifRGBImage if it owns it. +class RGBImageCleanup +{ +public: + RGBImageCleanup(avifRGBImage * rgb) : rgb_(rgb) {} + ~RGBImageCleanup() { avifRGBImageFreePixels(rgb_); } + +private: + avifRGBImage * rgb_ = nullptr; +}; } // namespace avif diff --git a/tests/gtest/avifbasictest.cc b/tests/gtest/avifbasictest.cc index 7a08621e7a..9057ce3b74 100644 --- a/tests/gtest/avifbasictest.cc +++ b/tests/gtest/avifbasictest.cc @@ -41,5 +41,17 @@ TEST(BasicTest, EncodeDecode) { // testutil::WriteImage(decoded.get(), "/tmp/avifbasictest.png"); } +TEST(BasicTest, SharedPtr) { + // Make sure the following does not create sanitizer bugs. + ImagePtr image = + testutil::CreateImage(/*width=*/12, /*height=*/34, /*depth=*/8, + AVIF_PIXEL_FORMAT_YUV420, AVIF_PLANES_ALL); + + std::unique_ptr rgb(new avifRGBImage()); + avifRGBImageSetDefaults(rgb.get(), image.get()); + ASSERT_EQ(avifRGBImageAllocatePixels(rgb.get()), AVIF_RESULT_OK); + RGBImageCleanup(rgb.get()); +} + } // namespace } // namespace avif From a9d6c04458a6ecabad77a4c166ec2e99952d38c4 Mon Sep 17 00:00:00 2001 From: Wan-Teh Chang Date: Thu, 26 Feb 2026 16:43:59 -0800 Subject: [PATCH 2/4] Delete "if it owns it" from comment --- include/avif/avif_cxx.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/avif/avif_cxx.h b/include/avif/avif_cxx.h index fc4cfd7d87..2de1754646 100644 --- a/include/avif/avif_cxx.h +++ b/include/avif/avif_cxx.h @@ -30,7 +30,7 @@ using EncoderPtr = std::unique_ptr; using GainMapPtr = std::unique_ptr; using ImagePtr = std::unique_ptr; -// Automatically cleans the ressources of the avifRGBImage if it owns it. +// Automatically cleans the ressources of the avifRGBImage. class RGBImageCleanup { public: From 753fb8f2e13ac75e854f692d0f8f686af0bb2698 Mon Sep 17 00:00:00 2001 From: Wan-Teh Chang Date: Thu, 26 Feb 2026 16:48:45 -0800 Subject: [PATCH 3/4] Rename test, declare RGBImageCleanup local variable --- tests/gtest/avifbasictest.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/gtest/avifbasictest.cc b/tests/gtest/avifbasictest.cc index 9057ce3b74..ef3031113a 100644 --- a/tests/gtest/avifbasictest.cc +++ b/tests/gtest/avifbasictest.cc @@ -41,16 +41,16 @@ TEST(BasicTest, EncodeDecode) { // testutil::WriteImage(decoded.get(), "/tmp/avifbasictest.png"); } -TEST(BasicTest, SharedPtr) { +TEST(BasicTest, RGBImageCleanup) { // Make sure the following does not create sanitizer bugs. ImagePtr image = testutil::CreateImage(/*width=*/12, /*height=*/34, /*depth=*/8, AVIF_PIXEL_FORMAT_YUV420, AVIF_PLANES_ALL); - std::unique_ptr rgb(new avifRGBImage()); - avifRGBImageSetDefaults(rgb.get(), image.get()); - ASSERT_EQ(avifRGBImageAllocatePixels(rgb.get()), AVIF_RESULT_OK); - RGBImageCleanup(rgb.get()); + avifRGBImage rgb; + avifRGBImageSetDefaults(&rgb, image.get()); + RGBImageCleanup cleanup(&rgb); + ASSERT_EQ(avifRGBImageAllocatePixels(&rgb), AVIF_RESULT_OK); } } // namespace From 47ce5b9b16753bce592e8331284b268a1822e48c Mon Sep 17 00:00:00 2001 From: Wan-Teh Chang Date: Thu, 26 Feb 2026 16:57:52 -0800 Subject: [PATCH 4/4] Restore the original comment --- include/avif/avif_cxx.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/avif/avif_cxx.h b/include/avif/avif_cxx.h index 2de1754646..7a7723c5ed 100644 --- a/include/avif/avif_cxx.h +++ b/include/avif/avif_cxx.h @@ -31,6 +31,7 @@ using GainMapPtr = std::unique_ptr; using ImagePtr = std::unique_ptr; // Automatically cleans the ressources of the avifRGBImage. +// To use when RGBImage actually owns the pixels. RGBImage can also be used as a view, in which case it does not own the pixels. class RGBImageCleanup { public: