fix webp mem leak

This commit is contained in:
Green Sky 2024-03-11 20:45:36 +01:00
parent 3cf3097094
commit dc081ae2aa
No known key found for this signature in database
2 changed files with 17 additions and 11 deletions

View File

@ -1,5 +1,6 @@
#include "./image_loader_webp.hpp" #include "./image_loader_webp.hpp"
#include <memory>
#include <webp/demux.h> #include <webp/demux.h>
#include <webp/mux.h> #include <webp/mux.h>
#include <webp/encode.h> #include <webp/encode.h>
@ -21,13 +22,16 @@ ImageLoaderWebP::ImageInfo ImageLoaderWebP::loadInfoFromMemory(const uint8_t* da
// Tune 'dec_options' as needed. // Tune 'dec_options' as needed.
dec_options.color_mode = MODE_RGBA; dec_options.color_mode = MODE_RGBA;
WebPAnimDecoder* dec = WebPAnimDecoderNew(&webp_data, &dec_options); std::unique_ptr<WebPAnimDecoder, decltype(&WebPAnimDecoderDelete)> dec{
if (dec == nullptr) { WebPAnimDecoderNew(&webp_data, &dec_options),
&WebPAnimDecoderDelete
};
if (!static_cast<bool>(dec)) {
return res; return res;
} }
WebPAnimInfo anim_info; WebPAnimInfo anim_info;
WebPAnimDecoderGetInfo(dec, &anim_info); WebPAnimDecoderGetInfo(dec.get(), &anim_info);
res.width = anim_info.canvas_width; res.width = anim_info.canvas_width;
res.height = anim_info.canvas_height; res.height = anim_info.canvas_height;
res.file_ext = "webp"; res.file_ext = "webp";
@ -48,22 +52,25 @@ ImageLoaderWebP::ImageResult ImageLoaderWebP::loadFromMemoryRGBA(const uint8_t*
// Tune 'dec_options' as needed. // Tune 'dec_options' as needed.
dec_options.color_mode = MODE_RGBA; dec_options.color_mode = MODE_RGBA;
WebPAnimDecoder* dec = WebPAnimDecoderNew(&webp_data, &dec_options); std::unique_ptr<WebPAnimDecoder, decltype(&WebPAnimDecoderDelete)> dec{
if (dec == nullptr) { WebPAnimDecoderNew(&webp_data, &dec_options),
&WebPAnimDecoderDelete
};
if (!static_cast<bool>(dec)) {
return res; return res;
} }
WebPAnimInfo anim_info; WebPAnimInfo anim_info;
WebPAnimDecoderGetInfo(dec, &anim_info); WebPAnimDecoderGetInfo(dec.get(), &anim_info);
res.width = anim_info.canvas_width; res.width = anim_info.canvas_width;
res.height = anim_info.canvas_height; res.height = anim_info.canvas_height;
res.file_ext = "webp"; res.file_ext = "webp";
int prev_timestamp = 0; int prev_timestamp = 0;
while (WebPAnimDecoderHasMoreFrames(dec)) { while (WebPAnimDecoderHasMoreFrames(dec.get())) {
uint8_t* buf; uint8_t* buf;
int timestamp; int timestamp;
WebPAnimDecoderGetNext(dec, &buf, &timestamp); WebPAnimDecoderGetNext(dec.get(), &buf, &timestamp);
// ... (Render 'buf' based on 'timestamp'). // ... (Render 'buf' based on 'timestamp').
// ... (Do NOT free 'buf', as it is owned by 'dec'). // ... (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)); 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()); assert(anim_info.frame_count == res.frames.size());
return res; return res;

View File

@ -83,6 +83,7 @@ bool SendImagePopup::load(void) {
} }
} }
assert(preview_image.textures.empty());
preview_image.timestamp_last_rendered = Message::getTimeMS(); preview_image.timestamp_last_rendered = Message::getTimeMS();
preview_image.current_texture = 0; preview_image.current_texture = 0;
for (const auto& [ms, data] : original_image.frames) { for (const auto& [ms, data] : original_image.frames) {
@ -173,7 +174,7 @@ void SendImagePopup::render(float time_delta) {
// TODO: add cancel shortcut (esc) // TODO: add cancel shortcut (esc)
if (ImGui::BeginPopupModal("send image##SendImagePopup", nullptr/*, ImGuiWindowFlags_NoDecoration*/)) { 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(); const auto TEXT_BASE_HEIGHT = ImGui::GetTextLineHeightWithSpacing();
preview_image.doAnimation(Message::getTimeMS()); preview_image.doAnimation(Message::getTimeMS());