<div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif">From e33b7bc78be5d49dd510435981231a63f1c76aec Mon Sep 17 00:00:00 2001<br>From: Min Chen <<a href="mailto:chenm003@163.com">chenm003@163.com</a>><br>Date: Sat, 9 Nov 2024 12:58:49 +0530<br>Subject: [PATCH 2/3] Memory Leak Fix: Part 2<br><br>---<br> source/CMakeLists.txt            |   2 +-<br> source/abrEncApp.cpp             |   9 +-<br> source/common/param.cpp          | 140 ++++++++++++++++---------------<br> source/common/temporalfilter.cpp |   7 ++<br> source/common/temporalfilter.h   |   2 +-<br> source/common/threadpool.cpp     |   4 +-<br> source/encoder/analysis.cpp      |   6 +-<br> source/encoder/api.cpp           |  17 +++-<br> source/encoder/encoder.cpp       | 111 +++++++++++-------------<br> source/encoder/encoder.h         |   1 +<br> source/encoder/frameencoder.cpp  |  12 ++-<br> source/encoder/framefilter.cpp   |   8 +-<br> source/encoder/ratecontrol.cpp   |   4 +-<br> source/encoder/search.cpp        |   4 +-<br> source/encoder/slicetype.cpp     |  16 ++--<br> source/x265.cpp                  |  24 ++++--<br> source/x265.h                    |  28 ++++---<br> source/x265cli.cpp               |   7 +-<br> source/x265cli.h                 |   8 +-<br> 19 files changed, 210 insertions(+), 200 deletions(-)<br><br>diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt<br>index 310683909..14f6ccc49 100755<br>--- a/source/CMakeLists.txt<br>+++ b/source/CMakeLists.txt<br>@@ -31,7 +31,7 @@ option(NATIVE_BUILD "Target the build CPU" OFF)<br> option(STATIC_LINK_CRT "Statically link C and C++ runtimes for release builds" OFF)<br> mark_as_advanced(FPROFILE_USE FPROFILE_GENERATE NATIVE_BUILD)<br> # X265_BUILD must be incremented each time the public API is changed<br>-set(X265_BUILD 213)<br>+set(X265_BUILD 214)<br> configure_file("${PROJECT_SOURCE_DIR}/<a href="http://x265.def.in">x265.def.in</a>"<br>                "${PROJECT_BINARY_DIR}/x265.def")<br> configure_file("${PROJECT_SOURCE_DIR}/<a href="http://x265_config.h.in">x265_config.h.in</a>"<br>diff --git a/source/abrEncApp.cpp b/source/abrEncApp.cpp<br>index ebdd3b3ba..a56e14cde 100644<br>--- a/source/abrEncApp.cpp<br>+++ b/source/abrEncApp.cpp<br>@@ -282,8 +282,8 @@ namespace X265_NS {<br> <br>         m_param->analysisLoadReuseLevel = m_cliopt.loadLevel;<br>         m_param->analysisSaveReuseLevel = m_cliopt.saveLevel;<br>-        m_param->analysisSave = m_cliopt.saveLevel ? "save.dat" : NULL;<br>-        m_param->analysisLoad = m_cliopt.loadLevel ? "load.dat" : NULL;<br>+        strcpy(m_param->analysisSave, m_cliopt.saveLevel ? "save.dat" : "");<br>+        strcpy(m_param->analysisLoad, m_cliopt.loadLevel ? "load.dat" : "");<br>         m_param->bUseAnalysisFile = 0;<br> <br>         if (m_cliopt.loadLevel)<br>@@ -599,7 +599,7 @@ ret:<br>             x265_picture* pic_recon;<br>             x265_picture pic_out[MAX_LAYERS];<br> <br>-            pic_recon = (m_cliopt.recon[0] || m_param->analysisSave || m_param->analysisLoad || pts_queue || reconPlay || m_param->csvLogLevel) ? pic_out : NULL;<br>+            pic_recon = (m_cliopt.recon[0] || strlen(m_param->analysisSave) || strlen(m_param->analysisLoad) || pts_queue || reconPlay || m_param->csvLogLevel) ? pic_out : NULL;<br>             uint32_t inFrameCount = 0;<br>             uint32_t outFrameCount = 0;<br>             x265_nal *p_nal;<br>@@ -895,7 +895,7 @@ ret:<br>             delete reconPlay;<br> <br>             api->encoder_get_stats(m_encoder, &stats, sizeof(stats));<br>-            if (m_param->csvfn && !b_ctrl_c)<br>+            if (strlen(m_param->csvfn) && !b_ctrl_c)<br> #if ENABLE_LIBVMAF<br>                 api->vmaf_encoder_log(m_encoder, m_cliopt.argCnt, m_cliopt.argString, m_cliopt.param, vmafdata);<br> #else<br>@@ -919,7 +919,6 @@ ret:<br>             if (b_ctrl_c)<br>                 general_log(m_param, NULL, X265_LOG_INFO, "aborted at input frame %d, output frame %d in %s\n",<br>                     m_cliopt.seek + inFrameCount, stats.encodedPictureCount, profileName);<br>-            if(m_param->numaPools)    free((char*)m_param->numaPools);<br>             api->param_free(m_param);<br> <br>             X265_FREE(errorBuf);<br>diff --git a/source/common/param.cpp b/source/common/param.cpp<br>index 1beb3c056..010721315 100755<br>--- a/source/common/param.cpp<br>+++ b/source/common/param.cpp<br>@@ -94,6 +94,7 @@ namespace X265_NS {<br> x265_param *x265_param_alloc()<br> {<br>     x265_param* param = (x265_param*)x265_malloc(sizeof(x265_param));<br>+    memset(param, 0, sizeof(x265_param));<br> #ifdef SVT_HEVC<br>     param->svtHevcParam = (EB_H265_ENC_CONFIGURATION*)x265_malloc(sizeof(EB_H265_ENC_CONFIGURATION));<br> #endif<br>@@ -151,8 +152,8 @@ void x265_param_default(x265_param* param)<br> <br>     param->logLevel = X265_LOG_INFO;<br>     param->csvLogLevel = 0;<br>-    param->csvfn = NULL;<br>-    param->rc.lambdaFileName = NULL;<br>+    param->csvfn[0] = 0;<br>+    param->rc.lambdaFileName[0] = 0;<br>     param->bLogCuStats = 0;<br>     param->decodedPictureHashSEI = 0;<br> <br>@@ -279,9 +280,9 @@ void x265_param_default(x265_param* param)<br>     param->analysisReuseMode = 0; /*DEPRECATED*/<br>     param->analysisMultiPassRefine = 0;<br>     param->analysisMultiPassDistortion = 0;<br>-    param->analysisReuseFileName = NULL;<br>-    param->analysisSave = NULL;<br>-    param->analysisLoad = NULL;<br>+    param->analysisReuseFileName[0] = 0;<br>+    param->analysisSave[0] = 0;<br>+    param->analysisLoad[0] = 0;<br>     param->bIntraInBFrames = 1;<br>     param->bLossless = 0;<br>     param->bCULossless = 0;<br>@@ -317,8 +318,8 @@ void x265_param_default(x265_param* param)<br>     param->rc.bStatRead = 0;<br>     param->rc.bStatWrite = 0;<br>     param->rc.dataShareMode = X265_SHARE_MODE_FILE;<br>-    param->rc.statFileName = NULL;<br>-    param->rc.sharedMemName = NULL;<br>+    param->rc.statFileName[0] = 0;<br>+    param->rc.sharedMemName[0] = 0;<br>     param->rc.bEncFocusedFramesOnly = 0;<br>     param->rc.complexityBlur = 20;<br>     param->rc.qblur = 0.5;<br>@@ -362,7 +363,7 @@ void x265_param_default(x265_param* param)<br>     param->maxLuma = PIXEL_MAX;<br>     param->log2MaxPocLsb = 8;<br>     param->maxSlices = 1;<br>-    param->videoSignalTypePreset = NULL;<br>+    param->videoSignalTypePreset[0] = 0;<br> <br>     /*Conformance window*/<br>     param->confWinRightOffset = 0;<br>@@ -379,7 +380,7 @@ void x265_param_default(x265_param* param)<br>     param->analysisReuseLevel = 0;  /*DEPRECATED*/<br>     param->analysisSaveReuseLevel = 0;<br>     param->analysisLoadReuseLevel = 0;<br>-    param->toneMapFile = NULL;<br>+    param->toneMapFile[0] = 0;<br>     param->bDhdr10opt = 0;<br>     param->dolbyProfile = 0;<br>     param->bCTUInfo = 0;<br>@@ -396,7 +397,7 @@ void x265_param_default(x265_param* param)<br>     param->bDisableLookahead = 0;<br>     param->bCopyPicToFrame = 1;<br>     param->maxAUSizeFactor = 1;<br>-    param->naluFile = NULL;<br>+    param->naluFile[0] = 0;<br> <br>     /* DCT Approximations */<br>     param->bLowPassDct = 0;<br>@@ -842,7 +843,7 @@ int x265_zone_param_parse(x265_param* p, const char* name, const char* value)<br>             p->rdoqLevel = 0;<br>     }<br>     OPT("b-intra") p->bIntraInBFrames = atobool(value);<br>-    OPT("scaling-list") p->scalingLists = strdup(value);<br>+    OPT("scaling-list") snprintf(p->scalingLists, X265_MAX_STRING_SIZE, "%s", value);<br>     OPT("crf")<br>     {<br>         p->rc.rfConstant = atof(value);<br>@@ -1294,13 +1295,13 @@ int x265_param_parse(x265_param* p, const char* name, const char* value)<br>         p->rc.bStatRead = pass & 2;<br>         p->rc.dataShareMode = X265_SHARE_MODE_FILE;<br>     }<br>-    OPT("stats") p->rc.statFileName = strdup(value);<br>-    OPT("scaling-list") p->scalingLists = strdup(value);<br>-    OPT2("pools", "numa-pools") p->numaPools = strdup(value);<br>-    OPT("lambda-file") p->rc.lambdaFileName = strdup(value);<br>-    OPT("analysis-reuse-file") p->analysisReuseFileName = strdup(value);<br>+    OPT("stats") snprintf(p->rc.statFileName, X265_MAX_STRING_SIZE, "%s", value);<br>+    OPT("scaling-list") snprintf(p->scalingLists, X265_MAX_STRING_SIZE, "%s", value);<br>+    OPT2("pools", "numa-pools") snprintf(p->numaPools, X265_MAX_STRING_SIZE, "%s", value);<br>+    OPT("lambda-file") snprintf(p->rc.lambdaFileName, X265_MAX_STRING_SIZE, "%s", value);<br>+    OPT("analysis-reuse-file") snprintf(p->analysisReuseFileName, X265_MAX_STRING_SIZE, "%s", value);<br>     OPT("qg-size") p->rc.qgSize = atoi(value);<br>-    OPT("master-display") p->masteringDisplayColorVolume = strdup(value);<br>+    OPT("master-display") snprintf(p->masteringDisplayColorVolume, X265_MAX_STRING_SIZE, "%s", value);<br>     OPT("max-cll") bError |= sscanf(value, "%hu,%hu", &p->maxCLL, &p->maxFALL) != 2;<br>     OPT("min-luma") p->minLuma = (uint16_t)atoi(value);<br>     OPT("max-luma") p->maxLuma = (uint16_t)atoi(value);<br>@@ -1312,7 +1313,7 @@ int x265_param_parse(x265_param* p, const char* name, const char* value)<br>     if (bExtraParams)<br>     {<br>         if (0) ;<br>-        OPT("csv") p->csvfn = strdup(value);<br>+        OPT("csv") snprintf(p->csvfn, X265_MAX_STRING_SIZE, "%s", value);<br>         OPT("csv-log-level") p->csvLogLevel = atoi(value);<br>         OPT("qpmin") p->rc.qpMin = atoi(value);<br>         OPT("analyze-src-pics") p->bSourceReferenceEstimation = atobool(value);<br>@@ -1357,7 +1358,7 @@ int x265_param_parse(x265_param* p, const char* name, const char* value)<br>         OPT("hdr-opt") p->bHDR10Opt = atobool(value); /*DEPRECATED*/<br>         OPT("hdr10-opt") p->bHDR10Opt = atobool(value);<br>         OPT("limit-sao") p->bLimitSAO = atobool(value);<br>-        OPT("dhdr10-info") p->toneMapFile = strdup(value);<br>+        OPT("dhdr10-info") snprintf(p->toneMapFile, X265_MAX_STRING_SIZE, "%s", value);<br>         OPT("dhdr10-opt") p->bDhdr10opt = atobool(value);<br>         OPT("idr-recovery-sei") p->bEmitIDRRecoverySEI = atobool(value);<br>         OPT("const-vbv") p->rc.bEnableConstVbv = atobool(value);<br>@@ -1374,15 +1375,15 @@ int x265_param_parse(x265_param* p, const char* name, const char* value)<br>         OPT("copy-pic") p->bCopyPicToFrame = atobool(value);<br>         OPT("refine-analysis-type")<br>         {<br>-            if (strcmp(strdup(value), "avc") == 0)<br>+            if (strcmp((value), "avc") == 0)<br>             {<br>                 p->bAnalysisType = AVC_INFO;<br>             }<br>-            else if (strcmp(strdup(value), "hevc") == 0)<br>+            else if (strcmp((value), "hevc") == 0)<br>             {<br>                 p->bAnalysisType = HEVC_INFO;<br>             }<br>-            else if (strcmp(strdup(value), "off") == 0)<br>+            else if (strcmp((value), "off") == 0)<br>             {<br>                 p->bAnalysisType = DEFAULT;<br>             }<br>@@ -1392,8 +1393,8 @@ int x265_param_parse(x265_param* p, const char* name, const char* value)<br>             }<br>         }<br>         OPT("gop-lookahead") p->gopLookahead = atoi(value);<br>-        OPT("analysis-save") p->analysisSave = strdup(value);<br>-        OPT("analysis-load") p->analysisLoad = strdup(value);<br>+        OPT("analysis-save") snprintf(p->analysisSave, X265_MAX_STRING_SIZE, "%s", value);<br>+        OPT("analysis-load") snprintf(p->analysisLoad, X265_MAX_STRING_SIZE, "%s", value);<br>         OPT("radl") p->radl = atoi(value);<br>         OPT("max-ausize-factor") p->maxAUSizeFactor = atof(value);<br>         OPT("dynamic-refine") p->bDynamicRefine = atobool(value);<br>@@ -1402,7 +1403,7 @@ int x265_param_parse(x265_param* p, const char* name, const char* value)<br>         OPT("pic-struct") p->pictureStructure = atoi(value);<br>         OPT("chunk-start") p->chunkStart = atoi(value);<br>         OPT("chunk-end") p->chunkEnd = atoi(value);<br>-        OPT("nalu-file") p->naluFile = strdup(value);<br>+        OPT("nalu-file") snprintf(p->naluFile, X265_MAX_STRING_SIZE, "%s", value);<br>         OPT("dolby-vision-profile")<br>         {<br>             if (atof(value) < 10)<br>@@ -1478,7 +1479,7 @@ int x265_param_parse(x265_param* p, const char* name, const char* value)<br>         OPT("vbv-live-multi-pass") p->bliveVBV2pass = atobool(value);<br>         OPT("min-vbv-fullness") p->minVbvFullness = atof(value);<br>         OPT("max-vbv-fullness") p->maxVbvFullness = atof(value);<br>-        OPT("video-signal-type-preset") p->videoSignalTypePreset = strdup(value);<br>+        OPT("video-signal-type-preset") snprintf(p->videoSignalTypePreset, X265_MAX_STRING_SIZE, "%s", value);<br>         OPT("eob") p->bEnableEndOfBitstream = atobool(value);<br>         OPT("eos") p->bEnableEndOfSequence = atobool(value);<br>         /* Film grain characterstics model filename */<br>@@ -1866,11 +1867,11 @@ int x265_check_params(x265_param* param)<br>           "Constant QP is incompatible with 2pass");<br>     CHECK(param->rc.bStrictCbr && (param->rc.bitrate <= 0 || param->rc.vbvBufferSize <=0),<br>           "Strict-cbr cannot be applied without specifying target bitrate or vbv bufsize");<br>-    CHECK(param->analysisSave && (param->analysisSaveReuseLevel < 0 || param->analysisSaveReuseLevel > 10),<br>+    CHECK(strlen(param->analysisSave) && (param->analysisSaveReuseLevel < 0 || param->analysisSaveReuseLevel > 10),<br>         "Invalid analysis save refine level. Value must be between 1 and 10 (inclusive)");<br>-    CHECK(param->analysisLoad && (param->analysisLoadReuseLevel < 0 || param->analysisLoadReuseLevel > 10),<br>+    CHECK(strlen(param->analysisLoad) && (param->analysisLoadReuseLevel < 0 || param->analysisLoadReuseLevel > 10),<br>         "Invalid analysis load refine level. Value must be between 1 and 10 (inclusive)");<br>-    CHECK(param->analysisLoad && (param->mvRefine < 1 || param->mvRefine > 3),<br>+    CHECK(strlen(param->analysisLoad) && (param->mvRefine < 1 || param->mvRefine > 3),<br>         "Invalid mv refinement level. Value must be between 1 and 3 (inclusive)");<br>     CHECK(param->scaleFactor > 2, "Invalid scale-factor. Supports factor <= 2");<br>     CHECK(param->rc.qpMax < QP_MIN || param->rc.qpMax > QP_MAX_MAX,<br>@@ -1948,7 +1949,7 @@ int x265_check_params(x265_param* param)<br>         "SEA motion search does not support resolutions greater than 480p in 32 bit build");<br> #endif<br> <br>-    if (param->masteringDisplayColorVolume || param->maxFALL || param->maxCLL)<br>+    if (strlen(param->masteringDisplayColorVolume) || param->maxFALL || param->maxCLL)<br>         param->bEmitHDR10SEI = 1;<br> <br>     bool isSingleSEI = (param->bRepeatHeaders<br>@@ -1958,8 +1959,8 @@ int x265_check_params(x265_param* param)<br>                      || param->bEmitIDRRecoverySEI<br>                    || !!param->interlaceMode<br>                      || param->preferredTransferCharacteristics > 1<br>-                     || param->toneMapFile<br>-                     || param->naluFile);<br>+                     || strlen(param->toneMapFile)<br>+                     || strlen(param->naluFile));<br> <br>     if (!isSingleSEI && param->bSingleSeiNal)<br>     {<br>@@ -1990,7 +1991,7 @@ int x265_check_params(x265_param* param)<br>         CHECK((param->internalCsp != X265_CSP_I420), "Alpha encode supported only with i420a colorspace");<br>         CHECK((param->internalBitDepth > 10), "BitDepthConstraint must be 8 and 10  for Scalable main profile");<br>         CHECK((param->analysisMultiPassDistortion || param->analysisMultiPassRefine), "Alpha encode doesnot support multipass feature");<br>-        CHECK((param->analysisSave || param->analysisLoad), "Alpha encode doesnot support analysis save and load  feature");<br>+        CHECK((strlen(param->analysisSave) || strlen(param->analysisLoad)), "Alpha encode doesnot support analysis save and load  feature");<br>     }<br> #endif<br> #if ENABLE_MULTIVIEW<br>@@ -1999,7 +2000,7 @@ int x265_check_params(x265_param* param)<br>     {<br>         CHECK(param->internalBitDepth != 8, "BitDepthConstraint must be 8 for Multiview main profile");<br>         CHECK(param->analysisMultiPassDistortion || param->analysisMultiPassRefine, "Multiview encode doesnot support multipass feature");<br>-        CHECK(param->analysisSave || param->analysisLoad, "Multiview encode doesnot support analysis save and load feature");<br>+        CHECK(strlen(param->analysisSave) || strlen(param->analysisLoad), "Multiview encode doesnot support analysis save and load feature");<br>     }<br> #endif<br> #if ENABLE_SCC_EXT<br>@@ -2202,11 +2203,11 @@ char *x265_param2string(x265_param* p, int padx, int pady)<br> {<br>     char *buf, *s;<br>     size_t bufSize = 4000 + p->rc.zoneCount * 64;<br>-    if (p->numaPools)<br>+    if (strlen(p->numaPools))<br>         bufSize += strlen(p->numaPools);<br>-    if (p->masteringDisplayColorVolume)<br>+    if (strlen(p->masteringDisplayColorVolume))<br>         bufSize += strlen(p->masteringDisplayColorVolume);<br>-    if (p->videoSignalTypePreset)<br>+    if (strlen(p->videoSignalTypePreset))<br>         bufSize += strlen(p->videoSignalTypePreset);<br> <br>     buf = s = X265_MALLOC(char, bufSize);<br>@@ -2217,7 +2218,7 @@ char *x265_param2string(x265_param* p, int padx, int pady)<br> <br>     s += snprintf(s, bufSize - (s - buf), "cpuid=%d", p->cpuid);<br>     s += snprintf(s, bufSize - (s - buf), " frame-threads=%d", p->frameNumThreads);<br>-    if (p->numaPools)<br>+    if (strlen(p->numaPools))<br>         s += snprintf(s, bufSize - (s - buf), " numa-pools=%s", p->numaPools);<br>     BOOL(p->bEnableWavefront, "wpp");<br>     BOOL(p->bDistributeModeAnalysis, "pmode");<br>@@ -2225,7 +2226,7 @@ char *x265_param2string(x265_param* p, int padx, int pady)<br>     BOOL(p->bEnablePsnr, "psnr");<br>     BOOL(p->bEnableSsim, "ssim");<br>     s += snprintf(s, bufSize - (s - buf), " log-level=%d", p->logLevel);<br>-    if (p->csvfn)<br>+    if (strlen(p->csvfn))<br>         s += snprintf(s, bufSize - (s - buf), " csv csv-log-level=%d", p->csvLogLevel);<br>     s += snprintf(s, bufSize - (s - buf), " bitdepth=%d", p->internalBitDepth);<br>     s += snprintf(s, bufSize - (s - buf), " input-csp=%d", p->internalCsp);<br>@@ -2402,7 +2403,7 @@ char *x265_param2string(x265_param* p, int padx, int pady)<br>         s += snprintf(s, bufSize - (s - buf), " left=%d top=%d right=%d bottom=%d",<br>         p->vui.defDispWinLeftOffset, p->vui.defDispWinTopOffset,<br>         p->vui.defDispWinRightOffset, p->vui.defDispWinBottomOffset);<br>-    if (p->masteringDisplayColorVolume)<br>+    if (strlen(p->masteringDisplayColorVolume))<br>         s += snprintf(s, bufSize - (s - buf), " master-display=%s", p->masteringDisplayColorVolume);<br>     if (p->bEmitCLL)<br>         s += snprintf(s, bufSize - (s - buf), " cll=%hu,%hu", p->maxCLL, p->maxFALL);<br>@@ -2422,9 +2423,9 @@ char *x265_param2string(x265_param* p, int padx, int pady)<br>     BOOL(p->bHDR10Opt, "hdr10-opt");<br>     BOOL(p->bDhdr10opt, "dhdr10-opt");<br>     BOOL(p->bEmitIDRRecoverySEI, "idr-recovery-sei");<br>-    if (p->analysisSave)<br>+    if (strlen(p->analysisSave))<br>         s += snprintf(s, bufSize - (s - buf), " analysis-save");<br>-    if (p->analysisLoad)<br>+    if (strlen(p->analysisLoad))<br>         s += snprintf(s, bufSize - (s - buf), " analysis-load");<br>     s += snprintf(s, bufSize - (s - buf), " analysis-reuse-level=%d", p->analysisReuseLevel);<br>     s += snprintf(s, bufSize - (s - buf), " analysis-save-reuse-level=%d", p->analysisSaveReuseLevel);<br>@@ -2474,7 +2475,7 @@ char *x265_param2string(x265_param* p, int padx, int pady)<br> <br> bool parseLambdaFile(x265_param* param)<br> {<br>-    if (!param->rc.lambdaFileName)<br>+    if (!strlen(param->rc.lambdaFileName))<br>         return false;<br> <br>     FILE *lfn = x265_fopen(param->rc.lambdaFileName, "r");<br>@@ -2692,8 +2693,8 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     dst->mcstfFrameRange = src->mcstfFrameRange;<br>     dst->cpuid = src->cpuid;<br>     dst->frameNumThreads = src->frameNumThreads;<br>-    if (src->numaPools) dst->numaPools = strdup(src->numaPools);<br>-    else dst->numaPools = NULL;<br>+    if (strlen(src->numaPools)) snprintf(dst->numaPools, X265_MAX_STRING_SIZE, "%s", src->numaPools);<br>+    else dst->numaPools[0] = 0;<br> <br>     dst->bEnableWavefront = src->bEnableWavefront;<br>     dst->bDistributeModeAnalysis = src->bDistributeModeAnalysis;<br>@@ -2703,8 +2704,8 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     dst->bEnableSsim = src->bEnableSsim;<br>     dst->logLevel = src->logLevel;<br>     dst->csvLogLevel = src->csvLogLevel;<br>-    if (src->csvfn) dst->csvfn = strdup(src->csvfn);<br>-    else dst->csvfn = NULL;<br>+    if (strlen(src->csvfn)) snprintf(dst->csvfn, X265_MAX_STRING_SIZE, "%s", src->csvfn);<br>+    else dst->csvfn[0] = 0;<br>     dst->internalBitDepth = src->internalBitDepth;<br>     dst->sourceBitDepth = src->sourceBitDepth;<br>     dst->internalCsp = src->internalCsp;<br>@@ -2754,8 +2755,8 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     dst->bEnableTransformSkip = src->bEnableTransformSkip;<br>     dst->noiseReductionInter = src->noiseReductionInter;<br>     dst->noiseReductionIntra = src->noiseReductionIntra;<br>-    if (src->scalingLists) dst->scalingLists = strdup(src->scalingLists);<br>-    else dst->scalingLists = NULL;<br>+    if (strlen(src->scalingLists)) snprintf(dst->scalingLists, X265_MAX_STRING_SIZE, "%s", src->scalingLists);<br>+    else dst->scalingLists[0] = 0;<br>     dst->bEnableStrongIntraSmoothing = src->bEnableStrongIntraSmoothing;<br>     dst->bEnableConstrainedIntra = src->bEnableConstrainedIntra;<br>     dst->maxNumMergeCand = src->maxNumMergeCand;<br>@@ -2797,8 +2798,8 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     dst->psyRdoq = src->psyRdoq;<br>     dst->bEnableRdRefine = src->bEnableRdRefine;<br>     dst->analysisReuseMode = src->analysisReuseMode;<br>-    if (src->analysisReuseFileName) dst->analysisReuseFileName=strdup(src->analysisReuseFileName);<br>-    else dst->analysisReuseFileName = NULL;<br>+    if (strlen(src->analysisReuseFileName)) snprintf(dst->analysisReuseFileName, X265_MAX_STRING_SIZE, "%s", src->analysisReuseFileName);<br>+    else dst->analysisReuseFileName[0] = 0;<br>     dst->bLossless = src->bLossless;<br>     dst->cbQpOffset = src->cbQpOffset;<br>     dst->crQpOffset = src->crQpOffset;<br>@@ -2827,10 +2828,10 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     dst->rc.bStatWrite = src->rc.bStatWrite;<br>     dst->rc.bStatRead = src->rc.bStatRead;<br>     dst->rc.dataShareMode = src->rc.dataShareMode;<br>-    if (src->rc.statFileName) dst->rc.statFileName=strdup(src->rc.statFileName);<br>-    else dst->rc.statFileName = NULL;<br>-    if (src->rc.sharedMemName) dst->rc.sharedMemName = strdup(src->rc.sharedMemName);<br>-    else dst->rc.sharedMemName = NULL;<br>+    if (strlen(src->rc.statFileName)) snprintf(dst->rc.statFileName, X265_MAX_STRING_SIZE, "%s", src->rc.statFileName);<br>+    else dst->rc.statFileName[0] = 0;<br>+    if (strlen(src->rc.sharedMemName)) snprintf(dst->rc.sharedMemName, X265_MAX_STRING_SIZE, "%s", src->rc.sharedMemName);<br>+    else dst->rc.sharedMemName[0] = 0;<br>     dst->rc.qblur = src->rc.qblur;<br>     dst->rc.complexityBlur = src->rc.complexityBlur;<br>     dst->rc.bEnableSlowFirstPass = src->rc.bEnableSlowFirstPass;<br>@@ -2864,8 +2865,8 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     else<br>         dst->rc.zones = NULL;<br> <br>-    if (src->rc.lambdaFileName) dst->rc.lambdaFileName = strdup(src->rc.lambdaFileName);<br>-    else dst->rc.lambdaFileName = NULL;<br>+    if (strlen(src->rc.lambdaFileName)) snprintf(dst->rc.lambdaFileName, X265_MAX_STRING_SIZE, "%s", src->rc.lambdaFileName);<br>+    else dst->rc.lambdaFileName[0] = 0;<br>     dst->rc.bStrictCbr = src->rc.bStrictCbr;<br>     dst->rc.qgSize = src->rc.qgSize;<br>     dst->rc.bEnableGrain = src->rc.bEnableGrain;<br>@@ -2896,8 +2897,8 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     dst->vui.defDispWinRightOffset = src->vui.defDispWinRightOffset;<br>     dst->vui.defDispWinTopOffset = src->vui.defDispWinTopOffset;<br> <br>-    if (src->masteringDisplayColorVolume) dst->masteringDisplayColorVolume=strdup( src->masteringDisplayColorVolume);<br>-    else dst->masteringDisplayColorVolume = NULL;<br>+    if (strlen(src->masteringDisplayColorVolume)) snprintf(dst->masteringDisplayColorVolume, X265_MAX_STRING_SIZE, "%s", src->masteringDisplayColorVolume);<br>+    else dst->masteringDisplayColorVolume[0] = 0;<br>     dst->maxLuma = src->maxLuma;<br>     dst->minLuma = src->minLuma;<br>     dst->bEmitCLL = src->bEmitCLL;<br>@@ -2926,8 +2927,8 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     dst->analysisSaveReuseLevel = src->analysisSaveReuseLevel;<br>     dst->analysisLoadReuseLevel = src->analysisLoadReuseLevel;<br>     dst->bLimitSAO = src->bLimitSAO;<br>-    if (src->toneMapFile) dst->toneMapFile = strdup(src->toneMapFile);<br>-    else dst->toneMapFile = NULL;<br>+    if (strlen(src->toneMapFile)) snprintf(dst->toneMapFile, X265_MAX_STRING_SIZE, "%s", src->toneMapFile);<br>+    else dst->toneMapFile[0] = 0;<br>     dst->bDhdr10opt = src->bDhdr10opt;<br>     dst->bCTUInfo = src->bCTUInfo;<br>     dst->bUseRcStats = src->bUseRcStats;<br>@@ -2949,10 +2950,10 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     dst->vbvEndFrameAdjust = src->vbvEndFrameAdjust;<br>     dst->bAnalysisType = src->bAnalysisType;<br>     dst->bCopyPicToFrame = src->bCopyPicToFrame;<br>-    if (src->analysisSave) dst->analysisSave=strdup(src->analysisSave);<br>-    else dst->analysisSave = NULL;<br>-    if (src->analysisLoad) dst->analysisLoad=strdup(src->analysisLoad);<br>-    else dst->analysisLoad = NULL;<br>+    if (strlen(src->analysisSave)) snprintf(dst->analysisSave, X265_MAX_STRING_SIZE, "%s", src->analysisSave);<br>+    else dst->analysisSave[0] = 0;<br>+    if (strlen(src->analysisLoad)) snprintf(dst->analysisLoad, X265_MAX_STRING_SIZE, "%s", src->analysisLoad);<br>+    else dst->analysisLoad[0] = 0;<br>     dst->gopLookahead = src->gopLookahead;<br>     dst->radl = src->radl;<br>     dst->selectiveSAO = src->selectiveSAO;<br>@@ -2962,8 +2963,8 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     dst->bSingleSeiNal = src->bSingleSeiNal;<br>     dst->chunkStart = src->chunkStart;<br>     dst->chunkEnd = src->chunkEnd;<br>-    if (src->naluFile) dst->naluFile=strdup(src->naluFile);<br>-    else dst->naluFile = NULL;<br>+    if (src->naluFile) snprintf(dst->naluFile, X265_MAX_STRING_SIZE, "%s", src->naluFile);<br>+    else dst->naluFile[0] = 0;<br>     dst->scaleFactor = src->scaleFactor;<br>     dst->ctuDistortionRefine = src->ctuDistortionRefine;<br>     dst->bEnableHRDConcatFlag = src->bEnableHRDConcatFlag;<br>@@ -3001,8 +3002,8 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     dst->bEnableSCC = src->bEnableSCC;<br> #endif<br> <br>-    if (src->videoSignalTypePreset) dst->videoSignalTypePreset = strdup(src->videoSignalTypePreset);<br>-    else dst->videoSignalTypePreset = NULL;<br>+    if (strlen(src->videoSignalTypePreset)) snprintf(dst->videoSignalTypePreset, X265_MAX_STRING_SIZE, "%s", src->videoSignalTypePreset);<br>+    else dst->videoSignalTypePreset[0] = 0;<br> #ifdef SVT_HEVC<br>     memcpy(dst->svtHevcParam, src->svtHevcParam, sizeof(EB_H265_ENC_CONFIGURATION));<br> #endif<br>@@ -3236,6 +3237,7 @@ int svt_param_parse(x265_param* param, const char* name, const char* value)<br>                 svtHevcParam->logicalProcessors = atoi(temp1);<br>             }<br>         }<br>+        free(pools);<br>     }<br>     OPT("high-tier") svtHevcParam->tier = x265_atobool(value, bError);<br>     OPT("qpmin") svtHevcParam->minQpAllowed = atoi(value);<br>diff --git a/source/common/temporalfilter.cpp b/source/common/temporalfilter.cpp<br>index 215feaa0f..75b7f2188 100644<br>--- a/source/common/temporalfilter.cpp<br>+++ b/source/common/temporalfilter.cpp<br>@@ -153,6 +153,13 @@ TemporalFilter::TemporalFilter()<br>     m_motionVectorFactor = 16;<br> }<br> <br>+TemporalFilter::~TemporalFilter()<br>+{<br>+    if (m_metld)<br>+        delete m_metld;<br>+    predPUYuv.destroy();<br>+}<br>+<br> void TemporalFilter::init(const x265_param* param)<br> {<br>     m_param = param;<br>diff --git a/source/common/temporalfilter.h b/source/common/temporalfilter.h<br>index 723644633..bc7109b40 100644<br>--- a/source/common/temporalfilter.h<br>+++ b/source/common/temporalfilter.h<br>@@ -118,7 +118,7 @@ namespace X265_NS {<br>     {<br>     public:<br>         TemporalFilter();<br>-        ~TemporalFilter() {}<br>+        ~TemporalFilter();<br> <br>         void init(const x265_param* param);<br> <br>diff --git a/source/common/threadpool.cpp b/source/common/threadpool.cpp<br>index ab6bd0d43..b3505e5c0 100644<br>--- a/source/common/threadpool.cpp<br>+++ b/source/common/threadpool.cpp<br>@@ -302,7 +302,7 @@ ThreadPool* ThreadPool::allocThreadPools(x265_param* p, int& numPools, bool isTh<br>      * For windows because threads can't be allocated to live across sockets<br>      * changing the default behavior to be per-socket pools -- FIXME */<br> #if defined(_WIN32_WINNT) && _WIN32_WINNT >= _WIN32_WINNT_WIN7 || HAVE_LIBNUMA<br>-    if (!p->numaPools || (strcmp(p->numaPools, "NULL") == 0 || strcmp(p->numaPools, "*") == 0 || strcmp(p->numaPools, "") == 0))<br>+    if (!strlen(p->numaPools) || (strcmp(p->numaPools, "NULL") == 0 || strcmp(p->numaPools, "*") == 0 || strcmp(p->numaPools, "") == 0))<br>     {<br>          char poolString[50] = "";<br>          for (int i = 0; i < numNumaNodes; i++)<br>@@ -317,7 +317,7 @@ ThreadPool* ThreadPool::allocThreadPools(x265_param* p, int& numPools, bool isTh<br>          x265_param_parse(p, "pools", poolString);<br>      }<br> #endif<br>-    if (p->numaPools && *p->numaPools)<br>+    if (strlen(p->numaPools))<br>     {<br>         const char *nodeStr = p->numaPools;<br>         for (int i = 0; i < numNumaNodes; i++)<br>diff --git a/source/encoder/analysis.cpp b/source/encoder/analysis.cpp<br>index 6c72283f3..da5aeca77 100644<br>--- a/source/encoder/analysis.cpp<br>+++ b/source/encoder/analysis.cpp<br>@@ -203,7 +203,7 @@ Mode& Analysis::compressCTU(CUData& ctu, Frame& frame, const CUGeom& cuGeom, con<br>     }<br>     <br>     int reuseLevel = X265_MAX(m_param->analysisSaveReuseLevel, m_param->analysisLoadReuseLevel);<br>-    if ((m_param->analysisSave || m_param->analysisLoad) && m_slice->m_sliceType != I_SLICE && reuseLevel > 1 && reuseLevel < 10)<br>+    if ((strlen(m_param->analysisSave) || strlen(m_param->analysisLoad)) && m_slice->m_sliceType != I_SLICE && reuseLevel > 1 && reuseLevel < 10)<br>     {<br>         int numPredDir = m_slice->isInterP() ? 1 : 2;<br>         m_reuseInterDataCTU = m_frame->m_analysisData.interData;<br>@@ -217,7 +217,7 @@ Mode& Analysis::compressCTU(CUData& ctu, Frame& frame, const CUGeom& cuGeom, con<br>             m_reusePartSize = &m_reuseInterDataCTU->partSize[ctu.m_cuAddr * ctu.m_numPartitions];<br>             m_reuseMergeFlag = &m_reuseInterDataCTU->mergeFlag[ctu.m_cuAddr * ctu.m_numPartitions];<br>         }<br>-        if (m_param->analysisSave && !m_param->analysisLoad)<br>+        if (strlen(m_param->analysisSave) && !strlen(m_param->analysisLoad))<br>             for (int i = 0; i < X265_MAX_PRED_MODE_PER_CTU * numPredDir; i++)<br>                 m_reuseRef[i] = -1;<br>     }<br>@@ -4130,7 +4130,7 @@ int Analysis::calculateQpforCuSize(const CUData& ctu, const CUGeom& cuGeom, int3<br>     double qp = baseQp >= 0 ? baseQp : curEncData.m_cuStat[ctu.m_cuAddr].baseQp;<br>     bool bCuTreeOffset = IS_REFERENCED(m_frame) && m_param->rc.cuTree && !complexCheck;<br> <br>-    if ((m_param->analysisMultiPassDistortion && m_param->rc.bStatRead) || (m_param->ctuDistortionRefine && m_param->analysisLoad))<br>+    if ((m_param->analysisMultiPassDistortion && m_param->rc.bStatRead) || (m_param->ctuDistortionRefine && strlen(m_param->analysisLoad)))<br>     {<br>         x265_analysis_distortion_data* distortionData = m_frame->m_analysisData.distortionData;<br>         if ((distortionData->threshold[ctu.m_cuAddr] < 0.9 || distortionData->threshold[ctu.m_cuAddr] > 1.1)<br>diff --git a/source/encoder/api.cpp b/source/encoder/api.cpp<br>index 5d2a951e5..abcb2113c 100644<br>--- a/source/encoder/api.cpp<br>+++ b/source/encoder/api.cpp<br>@@ -204,6 +204,8 @@ x265_encoder *x265_encoder_open(x265_param *p)<br>         }<br>     }<br> <br>+    // Need free zone because follow up Memcpy will broken all of pointer<br>+    x265_zone_free(zoneParam);<br>     memcpy(zoneParam, param, sizeof(x265_param));<br>     for (int i = 0; i < param->rc.zonefileCount; i++)<br>     {<br>@@ -211,7 +213,7 @@ x265_encoder *x265_encoder_open(x265_param *p)<br>     }<br> <br>     /* Try to open CSV file handle */<br>-    if (encoder->m_param->csvfn)<br>+    if (strlen(encoder->m_param->csvfn))<br>     {<br>         encoder->m_param->csvfpt = x265_csvlog_open(encoder->m_param);<br>         if (!encoder->m_param->csvfpt)<br>@@ -221,6 +223,7 @@ x265_encoder *x265_encoder_open(x265_param *p)<br>         }<br>     }<br> <br>+    encoder->m_templateParam = param;<br>     encoder->m_latestParam = latestParam;<br>     encoder->m_zoneParam = zoneParam;<br>     x265_copy_params(latestParam, param);<br>@@ -309,7 +312,7 @@ int x265_encoder_reconfig(x265_encoder* enc, x265_param* param_in)<br>         return -1;<br>     x265_param save;<br>     Encoder* encoder = static_cast<Encoder*>(enc);<br>-    if (encoder->m_param->csvfn == NULL && param_in->csvfpt != NULL)<br>+    if (strlen(encoder->m_param->csvfn) && param_in->csvfpt != NULL)<br>          encoder->m_param->csvfpt = param_in->csvfpt;<br>     if (encoder->m_latestParam->forceFlush != param_in->forceFlush)<br>         return encoder->reconfigureParam(encoder->m_latestParam, param_in);<br>@@ -327,18 +330,20 @@ int x265_encoder_reconfig(x265_encoder* enc, x265_param* param_in)<br>     {<br>         /* reconfigure failed, recover saved param set */<br>         x265_copy_params(encoder->m_latestParam, &save);<br>+        x265_zone_free(&save);<br>         ret = -1;<br>     }<br>     else<br>     {<br>         encoder->configure(encoder->m_latestParam);<br>-        if (encoder->m_latestParam->scalingLists && encoder->m_latestParam->scalingLists != encoder->m_param->scalingLists)<br>+        if (strlen(encoder->m_latestParam->scalingLists) && strcmp(encoder->m_latestParam->scalingLists, encoder->m_param->scalingLists))<br>         {<br>             if (encoder->m_param->bRepeatHeaders)<br>             {<br>                 if (encoder->m_scalingList.parseScalingList(encoder->m_latestParam->scalingLists))<br>                 {<br>                     x265_copy_params(encoder->m_latestParam, &save);<br>+                    x265_zone_free(&save);<br>                     return -1;<br>                 }<br>                 encoder->m_scalingList.setupQuantMatrices(encoder->m_param->internalCsp);<br>@@ -347,6 +352,7 @@ int x265_encoder_reconfig(x265_encoder* enc, x265_param* param_in)<br>             {<br>                 x265_log(encoder->m_param, X265_LOG_ERROR, "Repeat headers is turned OFF, cannot reconfigure scalinglists\n");<br>                 x265_copy_params(encoder->m_latestParam, &save);<br>+                x265_zone_free(&save);<br>                 return -1;<br>             }<br>         }<br>@@ -373,6 +379,7 @@ int x265_encoder_reconfig(x265_encoder* enc, x265_param* param_in)<br>     /* Zones support modifying num of Refs. Requires determining level at each zone start*/<br>     if (encoder->m_param->rc.zonefileCount)<br>         determineLevel(*encoder->m_latestParam, encoder->m_vps);<br>+    x265_zone_free(&save);<br>     return ret;<br> }<br> <br>@@ -1009,7 +1016,7 @@ void x265_picture_init(x265_param *param, x265_picture *pic)<br>     pic->picStruct = 0;<br>     pic->vbvEndFlag = 0;<br> <br>-    if ((param->analysisSave || param->analysisLoad) || (param->bAnalysisType == AVC_INFO))<br>+    if ((strlen(param->analysisSave) || strlen(param->analysisLoad)) || (param->bAnalysisType == AVC_INFO))<br>     {<br>         uint32_t widthInCU = (param->sourceWidth + param->maxCUSize - 1) >> param->maxLog2CUSize;<br>         uint32_t heightInCU = (param->sourceHeight + param->maxCUSize - 1) >> param->maxLog2CUSize;<br>@@ -1041,6 +1048,8 @@ void x265_zone_free(x265_param *param)<br>     {<br>         for (int i = 0; i < param->rc.zonefileCount; i++)<br>             x265_free(param->rc.zones[i].zoneParam);<br>+        param->rc.zonefileCount = 0;<br>+        param->rc.zoneCount = 0;<br>         x265_free(param->rc.zones);<br>     }<br> }<br>diff --git a/source/encoder/encoder.cpp b/source/encoder/encoder.cpp<br>index 5f784e72b..d1d12e91c 100644<br>--- a/source/encoder/encoder.cpp<br>+++ b/source/encoder/encoder.cpp<br>@@ -138,6 +138,7 @@ Encoder::Encoder()<br>     m_outputCount = 0;<br>     m_param = NULL;<br>     m_latestParam = NULL;<br>+    m_templateParam = NULL;<br>     m_threadPool = NULL;<br>     m_analysisFileIn = NULL;<br>     m_analysisFileOut = NULL;<br>@@ -255,7 +256,7 @@ void Encoder::create()<br>         p->bEnableWavefront = 0;<br>     }<br> <br>-    bool allowPools = !p->numaPools || strcmp(p->numaPools, "none");<br>+    bool allowPools = !strlen(p->numaPools) || strcmp(p->numaPools, "none");<br> <br>     // Trim the thread pool if --wpp, --pme, and --pmode are disabled<br>     if (!p->bEnableWavefront && !p->bDistributeModeAnalysis && !p->bDistributeMotionEstimation && !p->lookaheadSlices)<br>@@ -336,7 +337,7 @@ void Encoder::create()<br>         m_aborted = true;<br>         return;<br>     }<br>-    else if (!m_param->scalingLists || !strcmp(m_param->scalingLists, "off"))<br>+    else if (!strlen(m_param->scalingLists) || !strcmp(m_param->scalingLists, "off"))<br>         m_scalingList.m_bEnabled = false;<br>     else if (!strcmp(m_param->scalingLists, "default"))<br>         m_scalingList.setDefaultScalingList();<br>@@ -478,7 +479,7 @@ void Encoder::create()<br>         m_aborted = true;<br> <br>     initRefIdx();<br>-    if (m_param->analysisSave && m_param->bUseAnalysisFile)<br>+    if (strlen(m_param->analysisSave) && m_param->bUseAnalysisFile)<br>     {<br>         char* temp = strcatFilename(m_param->analysisSave, ".temp");<br>         if (!temp)<br>@@ -498,7 +499,7 @@ void Encoder::create()<br>     if (m_param->analysisMultiPassRefine || m_param->analysisMultiPassDistortion)<br>     {<br>         const char* name = m_param->analysisReuseFileName;<br>-        if (!name)<br>+        if (!strlen(name))<br>             name = defaultAnalysisFileName;<br>         if (m_param->rc.bStatWrite)<br>         {<br>@@ -550,7 +551,7 @@ void Encoder::create()<br> <br>     m_nalList.m_annexB = !!m_param->bAnnexB;<br> <br>-    if (m_param->naluFile)<br>+    if (strlen(m_param->naluFile))<br>     {<br>         m_naluFile = x265_fopen(m_param->naluFile, "r");<br>         if (!m_naluFile)<br>@@ -951,19 +952,16 @@ void Encoder::destroy()<br> <br>     if (m_latestParam != NULL && m_latestParam != m_param)<br>     {<br>-        if (m_latestParam->scalingLists != m_param->scalingLists)<br>-            free((char*)m_latestParam->scalingLists);<br>-        if (m_latestParam->numaPools != m_param->numaPools)<br>-            free((char*)m_latestParam->numaPools);<br>-<br>         PARAM_NS::x265_param_free(m_latestParam);<br>     }<br>     if (m_zoneParam != NULL && m_zoneParam != m_param) {<br>-        if (m_zoneParam->numaPools != m_param->numaPools)<br>-            free((char*)m_zoneParam->numaPools);<br>-<br>         PARAM_NS::x265_param_free(m_zoneParam);<br>     }<br>+    if (m_templateParam != NULL && m_templateParam != m_param) {<br>+        // TODO: we don't free zone here because it is overwrite into m_zoneParam in x265_encoder_open<br>+        m_templateParam->rc.zonefileCount = m_templateParam->rc.zoneCount = 0;<br>+        PARAM_NS::x265_param_free(m_templateParam);<br>+    }<br>     if (m_analysisFileIn)<br>         fclose(m_analysisFileIn);<br> <br>@@ -971,8 +969,8 @@ void Encoder::destroy()<br>     {<br>         int bError = 1;<br>         fclose(m_analysisFileOut);<br>-        const char* name = m_param->analysisSave ? m_param->analysisSave : m_param->analysisReuseFileName;<br>-        if (!name)<br>+        const char* name = strlen(m_param->analysisSave) ? m_param->analysisSave : m_param->analysisReuseFileName;<br>+        if (!strlen(name))<br>             name = defaultAnalysisFileName;<br>         char* temp = strcatFilename(name, ".temp");<br>         if (temp)<br>@@ -1000,19 +998,6 @@ void Encoder::destroy()<br>     {<br>         if (m_param->csvfpt)<br>             fclose(m_param->csvfpt);<br>-        /* release string arguments that were strdup'd */<br>-        free((char*)m_param->rc.lambdaFileName);<br>-        free((char*)m_param->rc.statFileName);<br>-        free((char*)m_param->rc.sharedMemName);<br>-        free((char*)m_param->analysisReuseFileName);<br>-        free((char*)m_param->scalingLists);<br>-        free((char*)m_param->csvfn);<br>-        free((char*)m_param->numaPools);<br>-        free((char*)m_param->masteringDisplayColorVolume);<br>-        free((char*)m_param->toneMapFile);<br>-        free((char*)m_param->analysisSave);<br>-        free((char*)m_param->analysisLoad);<br>-        free((char*)m_param->videoSignalTypePreset);<br>         PARAM_NS::x265_param_free(m_param);<br>     }<br> }<br>@@ -1509,7 +1494,7 @@ int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)<br> <br>     if (*m_exportedPic)<br>     {<br>-        if (!m_param->bUseAnalysisFile && m_param->analysisSave)<br>+        if (!m_param->bUseAnalysisFile && strlen(m_param->analysisSave))<br>             x265_free_analysis_data(m_param, &m_exportedPic[0]->m_analysisData);<br> <br>         for (int i = 0; i < m_param->numLayers; i++)<br>@@ -1801,7 +1786,7 @@ int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)<br> <br>         /* In analysisSave mode, x265_analysis_data is allocated in inputPic and inFrame points to this */<br>         /* Load analysis data before lookahead->addPicture, since sliceType has been decided */<br>-        if (m_param->analysisLoad)<br>+        if (strlen(m_param->analysisLoad))<br>         {<br>             /* reads analysis data for the frame and allocates memory based on slicetype */<br>             static int paramBytes = CONF_OFFSET_BYTES;<br>@@ -1992,7 +1977,7 @@ int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)<br>                 x265_frame_stats* frameData = NULL;<br> <br>                 /* Free up inputPic->analysisData since it has already been used */<br>-                if ((m_param->analysisLoad && !m_param->analysisSave) || ((m_param->bAnalysisType == AVC_INFO) && slice->m_sliceType != I_SLICE))<br>+                if ((strlen(m_param->analysisLoad) && !strlen(m_param->analysisSave)) || ((m_param->bAnalysisType == AVC_INFO) && slice->m_sliceType != I_SLICE))<br>                     x265_free_analysis_data(m_param, &outFrame->m_analysisData);<br>                 if (pic_out)<br>                 {<br>@@ -2020,7 +2005,7 @@ int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)<br>                     }<br> <br>                     /* Dump analysis data from pic_out to file in save mode and free */<br>-                    if (m_param->analysisSave)<br>+                    if (strlen(m_param->analysisSave))<br>                     {<br>                         pic_out[sLayer].analysisData.poc = pic_out[sLayer].poc;<br>                         pic_out[sLayer].analysisData.sliceType = pic_out[sLayer].sliceType;<br>@@ -2140,7 +2125,7 @@ int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)<br> <br>                 if ((m_outputCount + 1) >= m_param->chunkStart)<br>                     finishFrameStats(outFrame, curEncoder, frameData, m_pocLast, sLayer);<br>-                if (m_param->analysisSave)<br>+                if (strlen(m_param->analysisSave))<br>                 {<br>                     pic_out[sLayer].analysisData.frameBits = frameData->bits;<br>                     if (!slice->isIntra())<br>@@ -2334,7 +2319,7 @@ int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)<br>                 int baseViewType = frameEnc[0]->m_lowres.sliceType;<br>                 frameEnc[layer]->m_encData->m_slice->m_origSliceType = IS_X265_TYPE_B(baseViewType) ? B_SLICE : (baseViewType == X265_TYPE_P) ? P_SLICE : I_SLICE;<br>             }<br>-            if (m_param->analysisLoad && m_param->bDisableLookahead)<br>+            if (strlen(m_param->analysisLoad) && m_param->bDisableLookahead)<br>             {<br>                 frameEnc[0]->m_dts = frameEnc[0]->m_analysisData.lookahead.dts;<br>                 frameEnc[0]->m_reorderedPts = frameEnc[0]->m_analysisData.lookahead.reorderedPts;<br>@@ -2409,7 +2394,7 @@ int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)<br> <br>             curEncoder->m_rce.encodeOrder = frameEnc[0]->m_encodeOrder = m_encodedFrameNum++;<br> <br>-            if (!m_param->analysisLoad || !m_param->bDisableLookahead)<br>+            if (!strlen(m_param->analysisLoad) || !m_param->bDisableLookahead)<br>             {<br>                 if (m_bframeDelay)<br>                 {<br>@@ -2424,7 +2409,7 @@ int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)<br>             }<br> <br>             /* Allocate analysis data before encode in save mode. This is allocated in frameEnc[0] */<br>-            if (m_param->analysisSave && !m_param->analysisLoad)<br>+            if (strlen(m_param->analysisSave) && !strlen(m_param->analysisLoad))<br>             {<br>                 x265_analysis_data* analysis = &frameEnc[0]->m_analysisData;<br>                 analysis->poc = frameEnc[0]->m_poc;<br>@@ -2618,8 +2603,8 @@ int Encoder::reconfigureParam(x265_param* encParam, x265_param* param)<br>         encParam->bEnableRectInter = param->bEnableRectInter;<br>         encParam->maxNumMergeCand = param->maxNumMergeCand;<br>         encParam->bIntraInBFrames = param->bIntraInBFrames;<br>-        if (param->scalingLists && !encParam->scalingLists)<br>-            encParam->scalingLists = strdup(param->scalingLists);<br>+        if (strlen(param->scalingLists) && !strlen(encParam->scalingLists))<br>+            snprintf(encParam->scalingLists, X265_MAX_STRING_SIZE, "%s", param->scalingLists);<br> <br>         encParam->rc.aqMode = param->rc.aqMode;<br>         encParam->rc.aqStrength = param->rc.aqStrength;<br>@@ -3442,7 +3427,7 @@ void Encoder::getStreamHeaders(NALList& list, Entropy& sbacCoder, Bitstream& bs)<br>             cllsei.writeSEImessages(bs, m_sps, NAL_UNIT_PREFIX_SEI, list, m_param->bSingleSeiNal);<br>         }<br> <br>-        if (m_param->masteringDisplayColorVolume)<br>+        if (strlen(m_param->masteringDisplayColorVolume))<br>         {<br>             SEIMasteringDisplayColorVolume mdsei;<br>             if (mdsei.parse(m_param->masteringDisplayColorVolume))<br>@@ -3785,8 +3770,8 @@ void Encoder::configureZone(x265_param *p, x265_param *zone)<br>         p->bEnableRectInter = zone->bEnableRectInter;<br>         p->maxNumMergeCand = zone->maxNumMergeCand;<br>         p->bIntraInBFrames = zone->bIntraInBFrames;<br>-        if (zone->scalingLists)<br>-            p->scalingLists = strdup(zone->scalingLists);<br>+        if (strlen(zone->scalingLists))<br>+            snprintf(p->scalingLists, X265_MAX_STRING_SIZE, "%s", zone->scalingLists);<br> <br>         p->rc.aqMode = zone->rc.aqMode;<br>         p->rc.aqStrength = zone->rc.aqStrength;<br>@@ -3883,15 +3868,15 @@ void Encoder::configureVideoSignalTypePreset(x265_param* p)<br>             p->bEmitHDR10SEI = 1;<br>             if (!strcmp(colorVolume, "P3D65x1000n0005"))<br>             {<br>-                p->masteringDisplayColorVolume = strdup("G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,5)");<br>+                snprintf(p->masteringDisplayColorVolume, X265_MAX_STRING_SIZE, "G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,5)");<br>             }<br>             else if (!strcmp(colorVolume, "P3D65x4000n005"))<br>             {<br>-                p->masteringDisplayColorVolume = strdup("G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(40000000,50)");<br>+                snprintf(p->masteringDisplayColorVolume, X265_MAX_STRING_SIZE, "G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(40000000,50)");<br>             }<br>             else if (!strcmp(colorVolume, "BT2100x108n0005"))<br>             {<br>-                p->masteringDisplayColorVolume = strdup("G(8500,39850)B(6550,2300)R(34000,146000)WP(15635,16450)L(10000000,1)");<br>+                snprintf(p->masteringDisplayColorVolume, X265_MAX_STRING_SIZE, "G(8500,39850)B(6550,2300)R(34000,146000)WP(15635,16450)L(10000000,1)");<br>             }<br>             else<br>             {<br>@@ -4107,37 +4092,37 @@ void Encoder::configure(x265_param *p)<br>         p->rc.rfConstantMin = 0;<br>     }<br> <br>-    if (p->analysisSaveReuseLevel && !p->analysisSave)<br>+    if (p->analysisSaveReuseLevel && !strlen(p->analysisSave))<br>     {<br>         x265_log(p, X265_LOG_WARNING, "analysis-save-reuse-level can be set only when analysis-save is enabled."<br>             " Resetting analysis-save-reuse-level to 0.\n");<br>         p->analysisSaveReuseLevel = 0;<br>     }<br> <br>-    if (p->analysisLoadReuseLevel && !p->analysisLoad)<br>+    if (p->analysisLoadReuseLevel && !strlen(p->analysisLoad))<br>     {<br>         x265_log(p, X265_LOG_WARNING, "analysis-load-reuse-level can be set only when analysis-load is enabled."<br>             " Resetting analysis-load-reuse-level to 0.\n");<br>         p->analysisLoadReuseLevel = 0;<br>     }<br> <br>-    if (p->analysisSave && !p->analysisSaveReuseLevel)<br>+    if (strlen(p->analysisSave) && !p->analysisSaveReuseLevel)<br>         p->analysisSaveReuseLevel = 5;<br> <br>-    if (p->analysisLoad && !p->analysisLoadReuseLevel)<br>+    if (strlen(p->analysisLoad) && !p->analysisLoadReuseLevel)<br>         p->analysisLoadReuseLevel = 5;<br> <br>-    if ((p->analysisLoad || p->analysisSave) && (p->bDistributeModeAnalysis || p->bDistributeMotionEstimation))<br>+    if ((strlen(p->analysisLoad) || p->analysisSave) && (p->bDistributeModeAnalysis || p->bDistributeMotionEstimation))<br>     {<br>         x265_log(p, X265_LOG_WARNING, "Analysis load/save options incompatible with pmode/pme, Disabling pmode/pme\n");<br>         p->bDistributeMotionEstimation = p->bDistributeModeAnalysis = 0;<br>     }<br> <br>-    if ((p->analysisLoad || p->analysisSave) && (p->analysisMultiPassRefine || p->analysisMultiPassDistortion))<br>+    if ((strlen(p->analysisLoad) || strlen(p->analysisSave)) && (p->analysisMultiPassRefine || p->analysisMultiPassDistortion))<br>     {<br>         x265_log(p, X265_LOG_WARNING, "Cannot use Analysis load/save option and multi-pass-opt-analysis/multi-pass-opt-distortion together,"<br>             "Disabling Analysis load/save and multi-pass-opt-analysis/multi-pass-opt-distortion\n");<br>-        p->analysisSave = p->analysisLoad = NULL;<br>+        p->analysisSave[0] = p->analysisLoad[0] = 0;<br>         p->analysisMultiPassRefine = p->analysisMultiPassDistortion = 0;<br>     }<br>     if (p->scaleFactor)<br>@@ -4190,12 +4175,12 @@ void Encoder::configure(x265_param *p)<br> <br>     if (p->ctuDistortionRefine == CTU_DISTORTION_INTERNAL)<br>     {<br>-        if (!p->analysisLoad && !p->analysisSave)<br>+        if (!strlen(p->analysisLoad) && !strlen(p->analysisSave))<br>         {<br>             x265_log(p, X265_LOG_WARNING, "refine-ctu-distortion 1 requires analysis save/load. Disabling refine-ctu-distortion\n");<br>             p->ctuDistortionRefine = 0;<br>         }<br>-        if (p->scaleFactor && p->analysisLoad)<br>+        if (p->scaleFactor && strlen(p->analysisLoad))<br>         {<br>             x265_log(p, X265_LOG_WARNING, "refine-ctu-distortion 1 cannot be enabled along with multi resolution analysis refinement. Disabling refine-ctu-distortion\n");<br>             p->ctuDistortionRefine = 0;<br>@@ -4317,7 +4302,7 @@ void Encoder::configure(x265_param *p)<br>     p->bEnableTSkipFast &= p->bEnableTransformSkip;<br>     p->bLimitSAO &= p->bEnableSAO;<br> <br>-    if (m_param->bUseAnalysisFile && m_param->analysisLoad && (p->confWinRightOffset || p->confWinBottomOffset))<br>+    if (m_param->bUseAnalysisFile && strlen(m_param->analysisLoad) && (p->confWinRightOffset || p->confWinBottomOffset))<br>         x265_log(p, X265_LOG_WARNING, "It is recommended not to set conformance window offset in file based analysis-load."<br>                                       " Offsets are shared in the analysis file already.\n");<br>     /* initialize the conformance window */<br>@@ -4328,7 +4313,7 @@ void Encoder::configure(x265_param *p)<br>     m_conformanceWindow.leftOffset = 0;<br> <br>     uint32_t padsize = 0;<br>-    if (m_param->analysisLoad && m_param->bUseAnalysisFile)<br>+    if (strlen(m_param->analysisLoad) && m_param->bUseAnalysisFile)<br>     {<br>         m_analysisFileIn = x265_fopen(m_param->analysisLoad, "rb");<br>         if (!m_analysisFileIn)<br>@@ -4459,19 +4444,19 @@ void Encoder::configure(x265_param *p)<br>         m_param->bEnableFrameDuplication = 0;<br>     }<br> #ifdef ENABLE_HDR10_PLUS<br>-    if (m_param->bDhdr10opt && m_param->toneMapFile == NULL)<br>+    if (m_param->bDhdr10opt && strlen(m_param->toneMapFile) == 0)<br>     {<br>         x265_log(p, X265_LOG_WARNING, "Disabling dhdr10-opt. dhdr10-info must be enabled.\n");<br>         m_param->bDhdr10opt = 0;<br>     }<br> <br>-    if (m_param->toneMapFile)<br>+    if (strlen(m_param->toneMapFile))<br>     {<br>         if (!x265_fopen(p->toneMapFile, "r"))<br>         {<br>             x265_log(p, X265_LOG_ERROR, "Unable to open tone-map file.\n");<br>             m_bToneMap = 0;<br>-            m_param->toneMapFile = NULL;<br>+            m_param->toneMapFile[0] = 0;<br>             m_aborted = true;<br>         }<br>         else<br>@@ -4480,11 +4465,11 @@ void Encoder::configure(x265_param *p)<br>     else<br>         m_bToneMap = 0;<br> #else<br>-    if (m_param->toneMapFile)<br>+    if (strlen(m_param->toneMapFile))<br>     {<br>         x265_log(p, X265_LOG_WARNING, "--dhdr10-info disabled. Enable HDR10_PLUS in cmake.\n");<br>         m_bToneMap = 0;<br>-        m_param->toneMapFile = NULL;<br>+        m_param->toneMapFile[0] = 0;<br>     }<br>     else if (m_param->bDhdr10opt)<br>     {<br>@@ -4648,10 +4633,10 @@ void Encoder::configure(x265_param *p)<br>         }<br>     }<br> <br>-    if (p->videoSignalTypePreset)     // Default disabled.<br>+    if (strlen(p->videoSignalTypePreset))     // Default disabled.<br>         configureVideoSignalTypePreset(p);<br> <br>-    if (m_param->toneMapFile || p->bHDR10Opt || p->bEmitHDR10SEI)<br>+    if (strlen(m_param->toneMapFile) || p->bHDR10Opt || p->bEmitHDR10SEI)<br>     {<br>         if (!p->bRepeatHeaders)<br>         {<br>@@ -6281,6 +6266,8 @@ void Encoder::readUserSeiFile(x265_sei_payload& seiMsg, int curPoc)<br>             x265_log(m_param, X265_LOG_WARNING, "SEI message for frame %d is not inserted. Will support only PREFIX SEI messages.\n", poc);<br>             break;<br>         }<br>+        if (base64Decode)<br>+            free(base64Decode);<br>     }<br> }<br> <br>diff --git a/source/encoder/encoder.h b/source/encoder/encoder.h<br>index 6289779e7..95c73de8a 100644<br>--- a/source/encoder/encoder.h<br>+++ b/source/encoder/encoder.h<br>@@ -209,6 +209,7 @@ public:<br>     x265_param*        m_param;<br>     x265_param*        m_latestParam;     // Holds latest param during a reconfigure<br>     x265_param*        m_zoneParam;<br>+    x265_param*        m_templateParam;<br>     RateControl*       m_rateControl;<br>     Lookahead*         m_lookahead;<br>     AdaptiveFrameDuplication* m_dupBuffer[DUP_BUFFER];      // picture buffer of size 2<br>diff --git a/source/encoder/frameencoder.cpp b/source/encoder/frameencoder.cpp<br>index b6f2d6ed1..95eef813b 100644<br>--- a/source/encoder/frameencoder.cpp<br>+++ b/source/encoder/frameencoder.cpp<br>@@ -109,8 +109,6 @@ void FrameEncoder::destroy()<br> <br>     if (m_param->bEnableTemporalFilter)<br>     {<br>-        delete m_frameEncTF->m_metld;<br>-<br>         for (int i = 0; i < (m_frameEncTF->m_range << 1); i++)<br>             m_frameEncTF->destroyRefPicInfo(&m_mcstfRefList[i]);<br> <br>@@ -368,7 +366,7 @@ void FrameEncoder::threadMain()<br>             while (!m_frame[0]->m_ctuInfo)<br>                 m_frame[0]->m_copied.wait();<br>         }<br>-        if ((m_param->bAnalysisType == AVC_INFO) && !m_param->analysisSave && !m_param->analysisLoad && !(IS_X265_TYPE_I(m_frame[0]->m_lowres.sliceType)))<br>+        if ((m_param->bAnalysisType == AVC_INFO) && !strlen(m_param->analysisSave) && !strlen(m_param->analysisLoad) && !(IS_X265_TYPE_I(m_frame[0]->m_lowres.sliceType)))<br>         {<br>             while (((m_frame[0]->m_analysisData.interData == NULL && m_frame[0]->m_analysisData.intraData == NULL) || (uint32_t)m_frame[0]->m_poc != m_frame[0]->m_analysisData.poc))<br>                 m_frame[0]->m_copyMVType.wait();<br>@@ -555,7 +553,7 @@ void FrameEncoder::compressFrame(int layer)<br>         m_cuStats.countWeightAnalyze++;<br>         ScopedElapsedTime time(m_cuStats.weightAnalyzeTime);<br> #endif<br>-        if (m_param->analysisLoad)<br>+        if (strlen(m_param->analysisLoad))<br>         {<br>             for (int list = 0; list < slice->isInterB() + 1; list++) <br>             {<br>@@ -580,7 +578,7 @@ void FrameEncoder::compressFrame(int layer)<br>     else<br>         slice->disableWeights();<br> <br>-    if (m_param->analysisSave && (bUseWeightP || bUseWeightB))<br>+    if (strlen(m_param->analysisSave) && (bUseWeightP || bUseWeightB))<br>         reuseWP = (WeightParam*)m_frame[layer]->m_analysisData.wt;<br>     // Generate motion references<br>     int numPredDir = slice->isInterP() ? 1 : slice->isInterB() ? 2 : 0;<br>@@ -594,7 +592,7 @@ void FrameEncoder::compressFrame(int layer)<br>             slice->m_refReconPicList[l][ref] = slice->m_refFrameList[l][ref]->m_reconPic[0];<br>             m_mref[l][ref].init(slice->m_refReconPicList[l][ref], w, *m_param);<br>         }<br>-        if (m_param->analysisSave && (bUseWeightP || bUseWeightB))<br>+        if (strlen(m_param->analysisSave) && (bUseWeightP || bUseWeightB))<br>         {<br>             for (int i = 0; i < (m_param->internalCsp != X265_CSP_I400 ? 3 : 1); i++)<br>                 *(reuseWP++) = slice->m_weightPredTable[l][0][i];<br>@@ -1656,7 +1654,7 @@ void FrameEncoder::processRowEncoder(int intRow, ThreadLocalData& tld, int layer<br>             /* TODO: use defines from slicetype.h for lowres block size */<br>             uint32_t block_y = (ctu->m_cuPelY >> m_param->maxLog2CUSize) * noOfBlocks;<br>             uint32_t block_x = (ctu->m_cuPelX >> m_param->maxLog2CUSize) * noOfBlocks;<br>-            if (!m_param->analysisLoad || !m_param->bDisableLookahead)<br>+            if (!strlen(m_param->analysisLoad) || !m_param->bDisableLookahead)<br>             {<br>                 cuStat.vbvCost = 0;<br>                 cuStat.intraVbvCost = 0;<br>diff --git a/source/encoder/framefilter.cpp b/source/encoder/framefilter.cpp<br>index 71dc42b2c..b58799afd 100644<br>--- a/source/encoder/framefilter.cpp<br>+++ b/source/encoder/framefilter.cpp<br>@@ -162,11 +162,9 @@ void FrameFilter::destroy()<br> <br>     if (m_parallelFilter)<br>     {<br>-        if (m_useSao)<br>-        {<br>-            for(int row = 0; row < m_numRows; row++)<br>-                m_parallelFilter[row].m_sao.destroy((row == 0 ? 1 : 0));<br>-        }<br>+        // NOTE: don't check m_useSao because it is dynamic controllable<br>+        for(int row = 0; row < m_numRows; row++)<br>+            m_parallelFilter[row].m_sao.destroy((row == 0 ? 1 : 0));<br> <br>         delete[] m_parallelFilter;<br>         m_parallelFilter = NULL;<br>diff --git a/source/encoder/ratecontrol.cpp b/source/encoder/ratecontrol.cpp<br>index e46896987..5908992ce 100644<br>--- a/source/encoder/ratecontrol.cpp<br>+++ b/source/encoder/ratecontrol.cpp<br>@@ -496,7 +496,7 @@ bool RateControl::init(const SPS& sps)<br>     {<br>         /* If the user hasn't defined the stat filename, use the default value */<br>         const char *fileName = m_param->rc.statFileName;<br>-        if (!fileName)<br>+        if (!strlen(fileName))<br>             fileName = s_defaultStatFileName;<br>         /* Load stat file and init 2pass algo */<br>         if (m_param->rc.bStatRead)<br>@@ -3329,7 +3329,7 @@ void RateControl::terminate()<br> void RateControl::destroy()<br> {<br>     const char *fileName = m_param->rc.statFileName;<br>-    if (!fileName)<br>+    if (!strlen(fileName))<br>         fileName = s_defaultStatFileName;<br> <br>     if (m_statFileOut)<br>diff --git a/source/encoder/search.cpp b/source/encoder/search.cpp<br>index e398843a8..2a324700d 100644<br>--- a/source/encoder/search.cpp<br>+++ b/source/encoder/search.cpp<br>@@ -2135,7 +2135,7 @@ void Search::singleMotionEstimation(Search& master, Mode& interMode, const Predi<br>     bool bLowresMVP = false;<br>     MV mvmin, mvmax, outmv, mvp = amvp[mvpIdx], mvp_lowres;<br> <br>-    if (!m_param->analysisSave && !m_param->analysisLoad) /* Prevents load/save outputs from diverging if lowresMV is not available */<br>+    if (!strlen(m_param->analysisSave) && !strlen(m_param->analysisLoad)) /* Prevents load/save outputs from diverging if lowresMV is not available */<br>     {<br>         MV lmv = getLowresMV(interMode.cu, pu, list, ref);<br>         int layer = m_param->numViews > 1 ? m_frame->m_viewId : (m_param->numScalableLayers > 1) ? m_frame->m_sLayerId : 0;<br>@@ -2476,7 +2476,7 @@ void Search::predInterSearch(Mode& interMode, const CUGeom& cuGeom, bool bChroma<br>                     MV mvmin, mvmax, outmv, mvp = amvp[mvpIdx], mvp_lowres;<br>                     bool bLowresMVP = false;<br> <br>-                    if (!m_param->analysisSave && !m_param->analysisLoad) /* Prevents load/save outputs from diverging when lowresMV is not available */<br>+                    if (!strlen(m_param->analysisSave) && !strlen(m_param->analysisLoad)) /* Prevents load/save outputs from diverging when lowresMV is not available */<br>                     {<br>                         MV lmv = getLowresMV(cu, pu, list, ref);<br>                         int layer = m_param->numViews > 1 ? m_frame->m_viewId : (m_param->numScalableLayers > 1) ? m_frame->m_sLayerId : 0;<br>diff --git a/source/encoder/slicetype.cpp b/source/encoder/slicetype.cpp<br>index 046cf1596..f44c11ac4 100644<br>--- a/source/encoder/slicetype.cpp<br>+++ b/source/encoder/slicetype.cpp<br>@@ -1194,7 +1194,7 @@ void Lookahead::destroy()<br> /* Called by API thread */<br> void Lookahead::addPicture(Frame& curFrame, int sliceType)<br> {<br>-    if (m_param->analysisLoad && m_param->bDisableLookahead)<br>+    if (strlen(m_param->analysisLoad) && m_param->bDisableLookahead)<br>     {<br>         if (!m_filled)<br>             m_filled = true;<br>@@ -1295,7 +1295,7 @@ Frame* Lookahead::getDecidedPicture()<br>             return out;<br>         }<br> <br>-        if (m_param->analysisLoad && m_param->bDisableLookahead)<br>+        if (strlen(m_param->analysisLoad) && m_param->bDisableLookahead)<br>             return NULL;<br> <br>         findJob(-1); /* run slicetypeDecide() if necessary */<br>@@ -1365,14 +1365,14 @@ void Lookahead::getEstimatedPictureCost(Frame *curFrame)<br>     default:<br>         return;<br>     }<br>-    if (!curFrame->m_param->analysisLoad || !curFrame->m_param->bDisableLookahead)<br>+    if (!strlen(curFrame->m_param->analysisLoad) || !curFrame->m_param->bDisableLookahead)<br>     {<br>         X265_CHECK(curFrame->m_lowres.costEst[b - p0][p1 - b] > 0, "Slice cost not estimated\n")<br> <br>         if (curFrame->m_param->rc.cuTree && !curFrame->m_param->rc.bStatRead)<br>             /* update row satds based on cutree offsets */<br>             curFrame->m_lowres.satdCost = frameCostRecalculate(frames, p0, p1, b);<br>-        else if (!curFrame->m_param->analysisLoad || curFrame->m_param->scaleFactor || curFrame->m_param->bAnalysisType == HEVC_INFO)<br>+        else if (!strlen(curFrame->m_param->analysisLoad) || curFrame->m_param->scaleFactor || curFrame->m_param->bAnalysisType == HEVC_INFO)<br>         {<br>             if (curFrame->m_param->rc.aqMode)<br>                 curFrame->m_lowres.satdCost = curFrame->m_lowres.costEstAq[b - p0][p1 - b];<br>@@ -1909,7 +1909,7 @@ void Lookahead::slicetypeDecide()<br>         if (!m_param->rc.bStatRead)<br>             slicetypeAnalyse(frames, false);<br>         bool bIsVbv = m_param->rc.vbvBufferSize > 0 && m_param->rc.vbvMaxBitrate > 0;<br>-        if ((m_param->analysisLoad && m_param->scaleFactor && bIsVbv) || m_param->bliveVBV2pass)<br>+        if ((strlen(m_param->analysisLoad) && m_param->scaleFactor && bIsVbv) || m_param->bliveVBV2pass)<br>         {<br>             int numFrames;<br>             for (numFrames = 0; numFrames < maxSearch; numFrames++)<br>@@ -1923,7 +1923,7 @@ void Lookahead::slicetypeDecide()<br>     }<br> <br>     int bframes, brefs;<br>-    if (!m_param->analysisLoad || m_param->bAnalysisType == HEVC_INFO)<br>+    if (!strlen(m_param->analysisLoad) || m_param->bAnalysisType == HEVC_INFO)<br>     {<br>         bool isClosedGopRadl = m_param->radl && (m_param->keyframeMax != m_param->keyframeMin);<br>         for (bframes = 0, brefs = 0;; bframes++)<br>@@ -2340,7 +2340,7 @@ void Lookahead::slicetypeDecide()<br>             if (!m_param->rc.bStatRead)<br>                 slicetypeAnalyse(frames, true);<br>             bool bIsVbv = m_param->rc.vbvBufferSize > 0 && m_param->rc.vbvMaxBitrate > 0;<br>-            if ((m_param->analysisLoad && m_param->scaleFactor && bIsVbv) || m_param->bliveVBV2pass)<br>+            if ((strlen(m_param->analysisLoad) && m_param->scaleFactor && bIsVbv) || m_param->bliveVBV2pass)<br>             {<br>                 int numFrames;<br>                 for (numFrames = 0; numFrames < maxSearch; numFrames++)<br>@@ -2485,7 +2485,7 @@ void Lookahead::slicetypeDecide()<br>             if (!m_param->rc.bStatRead)<br>                 slicetypeAnalyse(frames, true);<br>             bool bIsVbv = m_param->rc.vbvBufferSize > 0 && m_param->rc.vbvMaxBitrate > 0;<br>-            if ((m_param->analysisLoad && m_param->scaleFactor && bIsVbv) || m_param->bliveVBV2pass)<br>+            if ((strlen(m_param->analysisLoad) && m_param->scaleFactor && bIsVbv) || m_param->bliveVBV2pass)<br>             {<br>                 int numFrames;<br>                 for (numFrames = 0; numFrames < maxSearch; numFrames++)<br>diff --git a/source/x265.cpp b/source/x265.cpp<br>index c9d2513e2..f392d27bf 100644<br>--- a/source/x265.cpp<br>+++ b/source/x265.cpp<br>@@ -154,6 +154,9 @@ static bool parseAbrConfig(FILE* abrConfig, CLIOptions cliopt[], uint8_t numEnco<br>     char line[1024];<br>     char* argLine;<br> <br>+    char **argv = (char**)alloca(256 * sizeof(char *));<br>+    char *strPool = (char*)alloca(256 * X265_MAX_STRING_SIZE * sizeof(char));<br>+    int strPoolSize = 256 * X265_MAX_STRING_SIZE;<br>     for (uint32_t i = 0; i < numEncodes; i++)<br>     {<br>         if (fgets(line, sizeof(line), abrConfig) == NULL) {<br>@@ -168,7 +171,6 @@ static bool parseAbrConfig(FILE* abrConfig, CLIOptions cliopt[], uint8_t numEnco<br>         char* start = strchr(argLine, ' ');<br>         while (isspace((unsigned char)*start)) start++;<br>         int argc = 0;<br>-        char **argv = (char**)malloc(256 * sizeof(char *));<br>         // Adding a dummy string to avoid file parsing error<br>         argv[argc++] = (char *)"x265";<br> <br>@@ -193,15 +195,18 @@ static bool parseAbrConfig(FILE* abrConfig, CLIOptions cliopt[], uint8_t numEnco<br>         }<br>         else<br>         {<br>-            cliopt[i].encName = strdup(head[0]);<br>+            snprintf(cliopt[i].encName, X265_MAX_STRING_SIZE, "%s", head[0]);<br>             cliopt[i].loadLevel = atoi(head[1]);<br>-            cliopt[i].reuseName = strdup(head[2]);<br>+            snprintf(cliopt[i].reuseName, X265_MAX_STRING_SIZE, "%s", head[2]);<br>         }<br> <br>         char* token = strtok(start, " ");<br>         while (token)<br>         {<br>-            argv[argc++] = strdup(token);<br>+            argv[argc++] = strPool;<br>+            strPool += strlen(token);<br>+            strPoolSize -= strlen(token);<br>+            strcpy(argv[argc++], token);<br>             token = strtok(NULL, " ");<br>         }<br>         argv[argc] = NULL;<br>@@ -213,6 +218,7 @@ static bool parseAbrConfig(FILE* abrConfig, CLIOptions cliopt[], uint8_t numEnco<br>             exit(1);<br>         }<br>     }<br>+    X265_CHECK(strPoolSize >= 0, "string pool broken!");<br>     return true;<br> }<br> <br>@@ -319,11 +325,11 @@ int main(int argc, char **argv)<br>             if (abrEnc->m_passEnc[idx]->m_ret)<br>             {<br>                 if (isAbrLadder)<br>-                    x265_log(NULL, X265_LOG_INFO, "Error generating ABR-ladder \n");<br>-                ret = abrEnc->m_passEnc[idx]->m_ret;<br>-                threadsActive = 0;<br>-                break;<br>-            }<br>+                    x265_log(NULL, X265_LOG_INFO, "Error generating ABR-ladder \n");<br>+                ret = abrEnc->m_passEnc[idx]->m_ret;<br>+                threadsActive = 0;<br>+                break;<br>+            }<br>         }<br>     }<br> <br>diff --git a/source/x265.h b/source/x265.h<br>index 13c4a47e2..671e196f6 100644<br>--- a/source/x265.h<br>+++ b/source/x265.h<br>@@ -36,6 +36,8 @@ extern "C" {<br> #pragma warning(disable: 4201) // non-standard extension used (nameless struct/union)<br> #endif<br> <br>+#define X265_MAX_STRING_SIZE    (256)<br>+<br> /* x265_encoder:<br>  *      opaque handler for encoder */<br> typedef struct x265_encoder x265_encoder;<br>@@ -1141,7 +1143,7 @@ typedef struct x265_param<br>      *<br>      * Frame encoders are distributed between the available thread pools, and<br>      * the encoder will never generate more thread pools than frameNumThreads */<br>-    const char* numaPools;<br>+    char numaPools[X265_MAX_STRING_SIZE];<br> <br>     /* Enable wavefront parallel processing, greatly increases parallelism for<br>      * less than 1% compression efficiency loss. Requires a thread pool, enabled<br>@@ -1185,7 +1187,7 @@ typedef struct x265_param<br>      * per-slice statistics to this log file in encode order. Otherwise the<br>      * encoder will emit per-stream statistics into the log file when<br>      * x265_encoder_log is called (presumably at the end of the encode) */<br>-    const char* csvfn;<br>+    char      csvfn[X265_MAX_STRING_SIZE];<br> <br>     /*== Internal Picture Specification ==*/<br> <br>@@ -1465,7 +1467,7 @@ typedef struct x265_param<br>      * - all other strings indicate a filename containing custom scaling lists<br>      *   in the HM format. The encode will fail if the file is not parsed<br>      *   correctly. Custom lists must be signaled in the SPS. */<br>-    const char *scalingLists;<br>+    char scalingLists[X265_MAX_STRING_SIZE];<br> <br>     /*== Intra Coding Tools ==*/<br> <br>@@ -1650,7 +1652,7 @@ typedef struct x265_param<br>     int       analysisReuseMode;<br> <br>     /* Filename for multi-pass-opt-analysis/distortion. Default name is "x265_analysis.dat" */<br>-    const char* analysisReuseFileName;<br>+    char      analysisReuseFileName[X265_MAX_STRING_SIZE];<br> <br>     /*== Rate Control ==*/<br> <br>@@ -1767,7 +1769,7 @@ typedef struct x265_param<br> <br>         /* Filename of the 2pass output/input stats file, if unspecified the<br>          * encoder will default to using x265_2pass.log */<br>-        const char* statFileName;<br>+        char statFileName[X265_MAX_STRING_SIZE];<br> <br>         /* temporally blur quants */<br>         double    qblur;<br>@@ -1791,7 +1793,7 @@ typedef struct x265_param<br>          * are separated by comma, space or newline. Text after a hash (#) is<br>          * ignored. The lambda tables are process-global, so these new lambda<br>          * values will affect all encoders in the same process */<br>-        const char* lambdaFileName;<br>+        char lambdaFileName[X265_MAX_STRING_SIZE];<br> <br>         /* Enable stricter conditions to check bitrate deviations in CBR mode. May compromise<br>          * quality to maintain bitrate adherence */<br>@@ -1828,7 +1830,7 @@ typedef struct x265_param<br>         int       dataShareMode;<br> <br>         /* Unique shared memory name. Required if the shared memory mode enabled. NULL by default */<br>-        const char* sharedMemName;<br>+        char sharedMemName[X265_MAX_STRING_SIZE];<br> <br>     } rc;<br> <br>@@ -1933,7 +1935,7 @@ typedef struct x265_param<br>      * are unsigned 16bit integers and %u are unsigned 32bit integers. The SEI<br>      * includes X,Y display primaries for RGB channels, white point X,Y and<br>      * max,min luminance values. */<br>-    const char* masteringDisplayColorVolume;<br>+    char masteringDisplayColorVolume[X265_MAX_STRING_SIZE];<br> <br>     /* Maximum Content light level(MaxCLL), specified as integer that indicates the<br>      * maximum pixel intensity level in units of 1 candela per square metre of the<br>@@ -2022,7 +2024,7 @@ typedef struct x265_param<br>     int       bLimitSAO;<br> <br>     /* File containing the tone mapping information */<br>-    const char*     toneMapFile;<br>+    char      toneMapFile[X265_MAX_STRING_SIZE];<br> <br>     /* Insert tone mapping information only for IDR frames and when the <br>      * tone mapping information changes. */<br>@@ -2098,11 +2100,11 @@ typedef struct x265_param<br>     int       gopLookahead;<br> <br>     /*Write per-frame analysis information into analysis buffers. Default disabled. */<br>-    const char* analysisSave;<br>+    char analysisSave[X265_MAX_STRING_SIZE];<br> <br>     /* Read analysis information into analysis buffer and use this analysis information<br>      * to reduce the amount of work the encoder must perform. Default disabled. */<br>-    const char* analysisLoad;<br>+    char analysisLoad[X265_MAX_STRING_SIZE];<br> <br>     /*Number of RADL pictures allowed in front of IDR*/<br>     int radl;<br>@@ -2134,7 +2136,7 @@ typedef struct x265_param<br>     * Default 0 (disabled). */<br>     int       chunkEnd;<br>     /* File containing base64 encoded SEI messages in POC order */<br>-    const char*    naluFile;<br>+    char      naluFile[X265_MAX_STRING_SIZE];<br> <br>     /* Generate bitstreams confirming to the specified dolby vision profile,<br>      * note that 0x7C01 makes RPU appear to be an unspecified NAL type in<br>@@ -2295,7 +2297,7 @@ typedef struct x265_param<br>     * precedence than individual VUI parameters. If any individual VUI option is specified<br>     * together with this, which changes the values set corresponding to the system-id<br>     * or color-volume, it will be discarded. */<br>-    const char* videoSignalTypePreset;<br>+    char     videoSignalTypePreset[X265_MAX_STRING_SIZE];<br> <br>     /* Flag indicating whether the encoder should emit an End of Bitstream<br>      * NAL at the end of bitstream. Default false */<br>diff --git a/source/x265cli.cpp b/source/x265cli.cpp<br>index 12df761cc..fd82ebd22 100755<br>--- a/source/x265cli.cpp<br>+++ b/source/x265cli.cpp<br>@@ -555,7 +555,6 @@ namespace X265_NS {<br>             showHelp(globalParam);<br>         }<br> <br>-        globalParam->rc.zones[zonefileCount].zoneParam = api->param_alloc();<br>         if (!globalParam->rc.zones[zonefileCount].zoneParam)<br>         {<br>             x265_log(NULL, X265_LOG_ERROR, "param alloc failed\n");<br>@@ -1131,6 +1130,7 @@ namespace X265_NS {<br>         }<br> <br>         rewind(zoneFile);<br>+        char **args = (char**)alloca(256 * sizeof(char *));<br>         param->rc.zones = X265_MALLOC(x265_zone, param->rc.zonefileCount);<br>         for (int i = 0; i < param->rc.zonefileCount; i++)<br>         {<br>@@ -1148,7 +1148,6 @@ namespace X265_NS {<br>                 start++;<br>                 param->rc.zones[i].startFrame = atoi(argLine);<br>                 int argCount = 0;<br>-                char **args = (char**)malloc(256 * sizeof(char *));<br>                 // Adding a dummy string to avoid file parsing error<br>                 args[argCount++] = (char *)"x265";<br>                 char* token = strtok(start, " ");<br>@@ -1354,6 +1353,7 @@ namespace X265_NS {<br>         rewind(multiViewConfig);<br>         int linenum = 0;<br>         int numInput = 0;<br>+        char** args = (char**)malloc(256 * sizeof(char*));<br>         while (fgets(line, sizeof(line), multiViewConfig))<br>         {<br>             if (*line == '#' || (strcmp(line, "\r\n") == 0))<br>@@ -1365,7 +1365,6 @@ namespace X265_NS {<br>             char* start = strchr(argLine, '-');<br>             int argCount = 0;<br>             char flag[] = "true";<br>-            char** args = (char**)malloc(256 * sizeof(char*));<br>             //Adding a dummy string to avoid file parsing error<br>             args[argCount++] = (char*)"x265";<br>             char* token = strtok(start, " ");<br>@@ -1467,10 +1466,12 @@ namespace X265_NS {<br>             {<br>                 if (api)<br>                     api->param_free(param);<br>+                free(args);<br>                 exit(1);<br>             }<br>             linenum++;<br>         }<br>+        free(args);<br>         if (numInput != (param->format ? 1 : param->numViews))<br>         {<br>             x265_log(NULL, X265_LOG_WARNING, "Number of Input files does not match with the given format <%d>\n", param->format);<br>diff --git a/source/x265cli.h b/source/x265cli.h<br>index 3df0fa7a6..aaa385cb4 100644<br>--- a/source/x265cli.h<br>+++ b/source/x265cli.h<br>@@ -434,8 +434,8 @@ static const struct option long_options[] =<br>         /* ABR ladder settings */<br>         bool isAbrLadderConfig;<br>         bool enableScaler;<br>-        char*    encName;<br>-        char*    reuseName;<br>+        char     encName[X265_MAX_STRING_SIZE];<br>+        char     reuseName[X265_MAX_STRING_SIZE];<br>         uint32_t encId;<br>         int      refId;<br>         uint32_t loadLevel;<br>@@ -471,8 +471,8 @@ static const struct option long_options[] =<br>             bDither = false;<br>             isAbrLadderConfig = false;<br>             enableScaler = false;<br>-            encName = NULL;<br>-            reuseName = NULL;<br>+            encName[0] = 0;<br>+            reuseName[0] = 0;<br>             encId = 0;<br>             refId = -1;<br>             loadLevel = 0;<br>-- <br>2.41.0.windows.1<br><br></div></div>