[x265] [PATCH] Fix memory leak in fpelMvCosts

vignesh at multicorewareinc.com vignesh at multicorewareinc.com
Wed Nov 30 10:40:37 CET 2016


# HG changeset patch
# User Vignesh Vijayakumar <vignesh at multicorewareinc.com>
# Date 1480475946 -19800
#      Wed Nov 30 08:49:06 2016 +0530
# Node ID 64bd8937a908a13326f16ac5da90195abc751568
# Parent  5ae077d7053d76a036d50fd426996cc2e2d3c565
Fix memory leak in fpelMvCosts

diff -r 5ae077d7053d -r 64bd8937a908 source/encoder/bitcost.cpp
--- a/source/encoder/bitcost.cpp	Mon Nov 28 11:35:49 2016 +0530
+++ b/source/encoder/bitcost.cpp	Wed Nov 30 08:49:06 2016 +0530
@@ -56,21 +56,29 @@
     }
     for (int j = 0; j < 4; j++)
     {
-         if (!s_fpelMvCosts[qp][j])
+        if (!s_fpelMvCosts[qp][j])
         {
-            s_fpelMvCosts[qp][j] = X265_MALLOC(uint16_t, BC_MAX_MV + 1) + (BC_MAX_MV >> 1);
+            ScopedLock s(s_costCalcLock);
+            if (!s_fpelMvCosts[qp][j])
+            {
+                s_fpelMvCosts[qp][j] = X265_MALLOC(uint16_t, BC_MAX_MV + 1) + (BC_MAX_MV >> 1);
+                if (!s_fpelMvCosts[qp][j])
+                {
+                    x265_log(NULL, X265_LOG_ERROR, "BitCost s_fpelMvCosts buffer allocation failure\n");
+                    return;
+                }
+                for (int i = -(BC_MAX_MV >> 1); i < (BC_MAX_MV >> 1); i++)
+                {
+                    s_fpelMvCosts[qp][j][i] = s_costs[qp][i * 4 + j];
+                }
+            }
         }
     }
-
+    m_cost = s_costs[qp];
     for (int j = 0; j < 4; j++)
     {
-        for (int i = -(BC_MAX_MV >> 1); i < (BC_MAX_MV >> 1); i++)
-        {
-            s_fpelMvCosts[qp][j][i] = s_costs[qp][i * 4 + j];
-        }
         m_fpelMvCosts[j] = s_fpelMvCosts[qp][j];
     }
-    m_cost = s_costs[qp];
 }
 
 /***
@@ -116,11 +124,12 @@
 
     for (int i = 0; i < BC_MAX_QP; i++)
     {
-        if (s_fpelMvCosts[i][0])
+        for (int j = 0; j < 4; j++)
         {
-            for (int j = 0; j < 4; j++)
+            if (s_fpelMvCosts[i][j])
             {
                 X265_FREE(s_fpelMvCosts[i][j] - (BC_MAX_MV >> 1));
+                s_fpelMvCosts[i][j] = NULL;
             }
         }
     }


More information about the x265-devel mailing list