<div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif">From 2bc2629ee6bde6d1ffdcf4bb9fde192db3b829e4 Mon Sep 17 00:00:00 2001<br>From: Min Chen <<a href="mailto:chenm003@163.com">chenm003@163.com</a>><br>Date: Mon, 11 Nov 2024 21:10:42 -0800<br>Subject: [PATCH] Memory Leak Fix: Part 3<br><br>---<br> source/encoder/api.cpp     |  1 -<br> source/encoder/bitcost.cpp |  9 +--------<br> source/encoder/bitcost.h   | 27 +++++++++++++++++++--------<br> 3 files changed, 20 insertions(+), 17 deletions(-)<br><br>diff --git a/source/encoder/api.cpp b/source/encoder/api.cpp<br>index 5b6e959c0..40a325334 100644<br>--- a/source/encoder/api.cpp<br>+++ b/source/encoder/api.cpp<br>@@ -993,7 +993,6 @@ void x265_free_analysis_data(x265_param *param, x265_analysis_data* analysis)<br> <br> void x265_cleanup(void)<br> {<br>-    BitCost::destroy();<br> }<br> <br> x265_picture *x265_picture_alloc()<br>diff --git a/source/encoder/bitcost.cpp b/source/encoder/bitcost.cpp<br>index 902417e62..a17c6dd68 100644<br>--- a/source/encoder/bitcost.cpp<br>+++ b/source/encoder/bitcost.cpp<br>@@ -84,14 +84,6 @@ void BitCost::setQP(unsigned int qp)<br>  * Class static data and methods<br>  */<br> <br>-uint16_t *BitCost::s_costs[BC_MAX_QP];<br>-<br>-uint16_t* BitCost::s_fpelMvCosts[BC_MAX_QP][4];<br>-<br>-float *BitCost::s_bitsizes;<br>-<br>-Lock BitCost::s_costCalcLock;<br>-<br> void BitCost::CalculateLogs()<br> {<br>     if (!s_bitsizes)<br>@@ -111,6 +103,7 @@ void BitCost::CalculateLogs()<br> <br> void BitCost::destroy()<br> {<br>+    ScopedLock s(s_costCalcLock);<br>     for (int i = 0; i < BC_MAX_QP; i++)<br>     {<br>         if (s_costs[i])<br>diff --git a/source/encoder/bitcost.h b/source/encoder/bitcost.h<br>index 46b232b42..425631056 100644<br>--- a/source/encoder/bitcost.h<br>+++ b/source/encoder/bitcost.h<br>@@ -35,7 +35,18 @@ class BitCost<br> {<br> public:<br> <br>-    BitCost() : m_cost_mvx(0), m_cost_mvy(0), m_cost(0), m_mvp(0) {}<br>+    BitCost()<br>+        : m_cost_mvx(0)<br>+        , m_cost_mvy(0)<br>+        , m_cost(0)<br>+        , m_mvp(0)<br>+        , s_bitsizes(NULL)<br>+    {<br>+        memset(m_fpelMvCosts, 0, sizeof(m_fpelMvCosts));<br>+        memset(s_costs, 0, sizeof(s_costs));<br>+        memset(s_fpelMvCosts, 0, sizeof(s_fpelMvCosts));<br>+    }<br>+    ~BitCost() { destroy(); }<br> <br>     void setQP(unsigned int qp);<br> <br>@@ -51,13 +62,13 @@ public:<br>                           s_bitsizes[mv.y - m_mvp.y] + 0.5f);<br>     }<br> <br>-    static inline uint32_t bitcost(const MV& mv, const MV& mvp)<br>+    inline uint32_t bitcost(const MV& mv, const MV& mvp) const<br>     {<br>         return (uint32_t)(s_bitsizes[mv.x - mvp.x] +<br>                           s_bitsizes[mv.y - mvp.y] + 0.5f);<br>     }<br> <br>-    static void destroy();<br>+    void destroy();<br> <br> protected:<br> <br>@@ -82,15 +93,15 @@ private:<br> <br>     enum { BC_MAX_QP = 82 };<br> <br>-    static float *s_bitsizes;<br>+    float *s_bitsizes;<br> <br>-    static uint16_t *s_costs[BC_MAX_QP];<br>+    uint16_t *s_costs[BC_MAX_QP];<br> <br>-    static uint16_t *s_fpelMvCosts[BC_MAX_QP][4];<br>+    uint16_t *s_fpelMvCosts[BC_MAX_QP][4];<br> <br>-    static Lock s_costCalcLock;<br>+    Lock s_costCalcLock;<br> <br>-    static void CalculateLogs();<br>+    void CalculateLogs();<br> };<br> }<br> <br>-- <br>2.41.0.windows.1<br><br></div></div>