From 342cea29bd9593fa89cbb33caff58055b46ebeec Mon Sep 17 00:00:00 2001 From: "chunmao.guo" Date: Fri, 12 Jul 2024 17:00:24 +0800 Subject: [PATCH] FIX: ffmpeg decoder memory leak Change-Id: I997572b5730618a969959f9b24c405d80fa9f83c Jira: STUDIO-7597 --- src/slic3r/GUI/AVVideoDecoder.cpp | 26 ++++++++++++-------------- src/slic3r/GUI/AVVideoDecoder.hpp | 1 + src/slic3r/GUI/wxMediaCtrl3.cpp | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/slic3r/GUI/AVVideoDecoder.cpp b/src/slic3r/GUI/AVVideoDecoder.cpp index cb9ed33a2..599b88381 100644 --- a/src/slic3r/GUI/AVVideoDecoder.cpp +++ b/src/slic3r/GUI/AVVideoDecoder.cpp @@ -81,17 +81,16 @@ bool AVVideoDecoder::toWxImage(wxImage &image, wxSize const &size2) size.GetWidth(), size.GetHeight(), wxFmt, SWS_POINT, // SWS_FAST_BILINEAR //SWS_BICUBIC nullptr, nullptr, nullptr); - uint8_t *data = (uint8_t*)malloc(size.GetWidth() * size.GetHeight() * 3); - if (data == nullptr) - return false; - uint8_t * datas[] = {data }; - int strides[] = {size.GetWidth() * 3}; + int length = size.GetWidth() * size.GetHeight() * 3; + if (bits_.size() < length) + bits_.resize(length); + uint8_t * datas[] = { bits_.data() }; + int strides[] = { size.GetWidth() * 3 }; int result_h = sws_scale(sws_ctx_, frame_->data, frame_->linesize, 0, frame_->height, datas, strides); if (result_h != size.GetHeight()) { - delete[] data; return false; } - image = wxImage(size.GetWidth(), size.GetHeight(), data); + image = wxImage(size.GetWidth(), size.GetHeight(), bits_.data()); return true; } @@ -111,16 +110,15 @@ bool AVVideoDecoder::toWxBitmap(wxBitmap &bitmap, wxSize const &size2) size.GetWidth(), size.GetHeight(), wxFmt, SWS_POINT, // SWS_FAST_BILINEAR //SWS_BICUBIC nullptr, nullptr, nullptr); - uint8_t *data = (uint8_t*)malloc(size.GetWidth() * size.GetHeight() * 4); - if (data == nullptr) - return false; - uint8_t *datas[] = {data}; - int strides[] = {size.GetWidth() * 4}; + int length = size.GetWidth() * size.GetHeight() * 4; + if (bits_.size() < length) + bits_.resize(length); + uint8_t *datas[] = { bits_.data() }; + int strides[] = { size.GetWidth() * 4 }; int result_h = sws_scale(sws_ctx_, frame_->data, frame_->linesize, 0, frame_->height, datas, strides); if (result_h != size.GetHeight()) { - delete[] data; return false; } - bitmap = wxBitmap((char *) data, size.GetWidth(), size.GetHeight(), 32); + bitmap = wxBitmap((char const *) bits_.data(), size.GetWidth(), size.GetHeight(), 32); return true; } diff --git a/src/slic3r/GUI/AVVideoDecoder.hpp b/src/slic3r/GUI/AVVideoDecoder.hpp index 7c6e5e7e7..a32241a62 100644 --- a/src/slic3r/GUI/AVVideoDecoder.hpp +++ b/src/slic3r/GUI/AVVideoDecoder.hpp @@ -34,6 +34,7 @@ private: AVFrame * frame_ = nullptr; SwsContext * sws_ctx_ = nullptr; bool got_frame_ = false; + std::vector bits_; }; #endif // AVVIDEODECODER_HPP diff --git a/src/slic3r/GUI/wxMediaCtrl3.cpp b/src/slic3r/GUI/wxMediaCtrl3.cpp index ca371314d..e326485bf 100644 --- a/src/slic3r/GUI/wxMediaCtrl3.cpp +++ b/src/slic3r/GUI/wxMediaCtrl3.cpp @@ -27,7 +27,7 @@ wxMediaCtrl3::wxMediaCtrl3(wxWindow *parent) , BambuLib(StaticBambuLib::get(this)) , m_thread([this] { PlayThread(); }) { - SetBackgroundColour(*wxBLACK); + SetBackgroundColour("#000001ff"); } wxMediaCtrl3::~wxMediaCtrl3()