From dc081ae2aa35d7649bd5e3a96768ba0eeb2fbc64 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Mon, 11 Mar 2024 20:45:36 +0100 Subject: [PATCH] fix webp mem leak --- src/image_loader_webp.cpp | 25 +++++++++++++++---------- src/send_image_popup.cpp | 3 ++- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/image_loader_webp.cpp b/src/image_loader_webp.cpp index 1748a4f..01cd9fb 100644 --- a/src/image_loader_webp.cpp +++ b/src/image_loader_webp.cpp @@ -1,5 +1,6 @@ #include "./image_loader_webp.hpp" +#include #include #include #include @@ -21,13 +22,16 @@ ImageLoaderWebP::ImageInfo ImageLoaderWebP::loadInfoFromMemory(const uint8_t* da // Tune 'dec_options' as needed. dec_options.color_mode = MODE_RGBA; - WebPAnimDecoder* dec = WebPAnimDecoderNew(&webp_data, &dec_options); - if (dec == nullptr) { + std::unique_ptr dec{ + WebPAnimDecoderNew(&webp_data, &dec_options), + &WebPAnimDecoderDelete + }; + if (!static_cast(dec)) { return res; } WebPAnimInfo anim_info; - WebPAnimDecoderGetInfo(dec, &anim_info); + WebPAnimDecoderGetInfo(dec.get(), &anim_info); res.width = anim_info.canvas_width; res.height = anim_info.canvas_height; res.file_ext = "webp"; @@ -48,22 +52,25 @@ ImageLoaderWebP::ImageResult ImageLoaderWebP::loadFromMemoryRGBA(const uint8_t* // Tune 'dec_options' as needed. dec_options.color_mode = MODE_RGBA; - WebPAnimDecoder* dec = WebPAnimDecoderNew(&webp_data, &dec_options); - if (dec == nullptr) { + std::unique_ptr dec{ + WebPAnimDecoderNew(&webp_data, &dec_options), + &WebPAnimDecoderDelete + }; + if (!static_cast(dec)) { return res; } WebPAnimInfo anim_info; - WebPAnimDecoderGetInfo(dec, &anim_info); + WebPAnimDecoderGetInfo(dec.get(), &anim_info); res.width = anim_info.canvas_width; res.height = anim_info.canvas_height; res.file_ext = "webp"; int prev_timestamp = 0; - while (WebPAnimDecoderHasMoreFrames(dec)) { + while (WebPAnimDecoderHasMoreFrames(dec.get())) { uint8_t* buf; int timestamp; - WebPAnimDecoderGetNext(dec, &buf, ×tamp); + WebPAnimDecoderGetNext(dec.get(), &buf, ×tamp); // ... (Render 'buf' based on 'timestamp'). // ... (Do NOT free 'buf', as it is owned by 'dec'). @@ -74,8 +81,6 @@ ImageLoaderWebP::ImageResult ImageLoaderWebP::loadFromMemoryRGBA(const uint8_t* new_frame.data.insert(new_frame.data.end(), buf, buf+(res.width*res.height*4)); } - WebPAnimDecoderDelete(dec); - assert(anim_info.frame_count == res.frames.size()); return res; diff --git a/src/send_image_popup.cpp b/src/send_image_popup.cpp index ea145fe..22b104e 100644 --- a/src/send_image_popup.cpp +++ b/src/send_image_popup.cpp @@ -83,6 +83,7 @@ bool SendImagePopup::load(void) { } } + assert(preview_image.textures.empty()); preview_image.timestamp_last_rendered = Message::getTimeMS(); preview_image.current_texture = 0; for (const auto& [ms, data] : original_image.frames) { @@ -173,7 +174,7 @@ void SendImagePopup::render(float time_delta) { // TODO: add cancel shortcut (esc) if (ImGui::BeginPopupModal("send image##SendImagePopup", nullptr/*, ImGuiWindowFlags_NoDecoration*/)) { - const auto TEXT_BASE_WIDTH = ImGui::CalcTextSize("A").x; + //const auto TEXT_BASE_WIDTH = ImGui::CalcTextSize("A").x; const auto TEXT_BASE_HEIGHT = ImGui::GetTextLineHeightWithSpacing(); preview_image.doAnimation(Message::getTimeMS());