shithub: openh264

Download patch

ref: d1c0a935c726baef86044ec1e1b697eb9eb70788
parent: 48c2fe176c94f0dd3957e73549a79bfaa6a7b29a
author: huili2 <huili2@cisco.com>
date: Wed Mar 8 17:04:58 EST 2017

fix low memory crash issue

--- a/codec/common/inc/WelsThreadPool.h
+++ b/codec/common/inc/WelsThreadPool.h
@@ -58,7 +58,7 @@
 
   static WELS_THREAD_ERROR_CODE SetThreadNum (int32_t iMaxThreadNum);
 
-  static CWelsThreadPool& AddReference();
+  static CWelsThreadPool* AddReference();
   void RemoveInstance();
 
   static bool IsReferenced();
@@ -102,6 +102,7 @@
   static int32_t   m_iRefCount;
   static CWelsLock m_cInitLock;
   static int32_t   m_iMaxThreadNum;
+  static CWelsThreadPool* m_pThreadPoolSelf;
 
   CWelsNonDuplicatedList<IWelsTask>* m_cWaitedTasks;
   CWelsNonDuplicatedList<CWelsTaskThread>* m_cIdleThreads;
--- a/codec/common/src/WelsThreadPool.cpp
+++ b/codec/common/src/WelsThreadPool.cpp
@@ -46,6 +46,7 @@
 int32_t CWelsThreadPool::m_iRefCount = 0;
 CWelsLock CWelsThreadPool::m_cInitLock;
 int32_t CWelsThreadPool::m_iMaxThreadNum = DEFAULT_THREAD_NUM;
+CWelsThreadPool* CWelsThreadPool::m_pThreadPoolSelf = NULL;
 
 CWelsThreadPool::CWelsThreadPool() :
   m_cWaitedTasks (NULL), m_cIdleThreads (NULL), m_cBusyThreads (NULL) {
@@ -75,13 +76,21 @@
 }
 
 
-CWelsThreadPool& CWelsThreadPool::AddReference () {
+CWelsThreadPool* CWelsThreadPool::AddReference() {
   CWelsAutoLock  cLock (m_cInitLock);
-  static CWelsThreadPool m_cThreadPoolSelf;
+  if (m_pThreadPoolSelf == NULL) {
+    m_pThreadPoolSelf = new CWelsThreadPool();
+    if (!m_pThreadPoolSelf) {
+      return NULL;
+    }
+  }
+
   if (m_iRefCount == 0) {
-    //TODO: will remove this afterwards
-    if (WELS_THREAD_ERROR_OK != m_cThreadPoolSelf.Init()) {
-      m_cThreadPoolSelf.Uninit();
+    if (WELS_THREAD_ERROR_OK != m_pThreadPoolSelf->Init()) {
+      m_pThreadPoolSelf->Uninit();
+      delete m_pThreadPoolSelf;
+      m_pThreadPoolSelf = NULL;
+      return NULL;
     }
   }
 
@@ -89,7 +98,7 @@
 
   ++ m_iRefCount;
   //fprintf(stdout, "m_iRefCount2=%d\n", m_iRefCount);
-  return m_cThreadPoolSelf;
+  return m_pThreadPoolSelf;
 }
 
 void CWelsThreadPool::RemoveInstance() {
@@ -99,6 +108,10 @@
   if (0 == m_iRefCount) {
     StopAllRunning();
     Uninit();
+    if (m_pThreadPoolSelf) {
+      delete m_pThreadPoolSelf;
+      m_pThreadPoolSelf = NULL;
+    }
     //fprintf(stdout, "m_iRefCount=%d, IdleThreadNum=%d, BusyThreadNum=%d, WaitedTask=%d\n", m_iRefCount, GetIdleThreadNum(), GetBusyThreadNum(), GetWaitedTaskNum());
   }
 }
@@ -106,7 +119,7 @@
 
 bool CWelsThreadPool::IsReferenced() {
   CWelsAutoLock  cLock (m_cInitLock);
-  return (m_iRefCount>0);
+  return (m_iRefCount > 0);
 }
 
 
@@ -137,7 +150,7 @@
   return WELS_THREAD_ERROR_OK;
 }
 
-WELS_THREAD_ERROR_CODE CWelsThreadPool::Init () {
+WELS_THREAD_ERROR_CODE CWelsThreadPool::Init() {
   //fprintf(stdout, "Enter WelsThreadPool Init\n");
 
   CWelsAutoLock  cLock (m_cLockPool);
@@ -197,9 +210,9 @@
 
   Kill();
 
-  WELS_DELETE_OP(m_cWaitedTasks);
-  WELS_DELETE_OP(m_cIdleThreads);
-  WELS_DELETE_OP(m_cBusyThreads);
+  WELS_DELETE_OP (m_cWaitedTasks);
+  WELS_DELETE_OP (m_cIdleThreads);
+  WELS_DELETE_OP (m_cBusyThreads);
 
   return iReturn;
 }
@@ -234,8 +247,8 @@
     }
   }
   //fprintf(stdout, "ThreadPool:  AddTaskToWaitedList: %x\n", pTask);
-  if (false == AddTaskToWaitedList (pTask)){
-      return WELS_THREAD_ERROR_GENERAL;
+  if (false == AddTaskToWaitedList (pTask)) {
+    return WELS_THREAD_ERROR_GENERAL;
   }
 
   //fprintf(stdout, "ThreadPool:  SignalThread: %x\n", pTask);
@@ -261,7 +274,7 @@
 
 void  CWelsThreadPool::DestroyThread (CWelsTaskThread* pThread) {
   pThread->Kill();
-  WELS_DELETE_OP(pThread);
+  WELS_DELETE_OP (pThread);
 
   return;
 }
@@ -292,7 +305,7 @@
 
   int32_t nRet = m_cWaitedTasks->push_back (pTask);
   //fprintf(stdout, "CWelsThreadPool::AddTaskToWaitedList=%d, pTask=%x\n", m_cWaitedTasks->size(), pTask);
-  return (0==nRet ? true : false);
+  return (0 == nRet ? true : false);
 }
 
 CWelsTaskThread*   CWelsThreadPool::GetIdleThread() {
--- a/codec/encoder/core/src/encoder_ext.cpp
+++ b/codec/encoder/core/src/encoder_ext.cpp
@@ -907,10 +907,10 @@
 
   (*ppCtx)->ppMbListD = static_cast<SMB**> ((*ppCtx)->pMemAlign->WelsMallocz (iNumDlayer * sizeof (SMB*), "ppMbListD"));
   (*ppCtx)->ppMbListD[0] = NULL;
-  WELS_VERIFY_RETURN_PROC_IF (1, (*ppCtx)->ppMbListD == NULL, FreeMemorySvc (ppCtx));
+  WELS_VERIFY_RETURN_IF (1, (*ppCtx)->ppMbListD == NULL)
   (*ppCtx)->ppMbListD[0] = static_cast<SMB*> ((*ppCtx)->pMemAlign->WelsMallocz (iOverallMbNum * sizeof (SMB),
                            "ppMbListD[0]"));
-  WELS_VERIFY_RETURN_PROC_IF (1, (*ppCtx)->ppMbListD[0] == NULL, FreeMemorySvc (ppCtx));
+  WELS_VERIFY_RETURN_IF (1, (*ppCtx)->ppMbListD[0] == NULL)
   (*ppCtx)->ppDqLayerList[0]->sMbDataP = (*ppCtx)->ppMbListD[0];
   InitMbInfo (*ppCtx, (*ppCtx)->ppMbListD[0], (*ppCtx)->ppDqLayerList[0], 0, iMbSize[iNumDlayer - 1]);
   for (i = 1; i < iNumDlayer; i++) {
@@ -1166,7 +1166,7 @@
 
     // pRef list
     pRefList = (SRefList*)pMa->WelsMallocz (sizeof (SRefList), "pRefList");
-    WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pRefList), FreeMemorySvc (ppCtx))
+    WELS_VERIFY_RETURN_IF (1, (NULL == pRefList))
     do {
       pRefList->pRef[i] = AllocPicture (pMa, kiWidth, kiHeight, true,
                                         (iDlayerIndex == iDlayerCount - 1) ? kiNeedFeatureStorage : 0); // to use actual size of current layer
@@ -1255,11 +1255,11 @@
     if (kiNeedFeatureStorage && iDlayerIndex == iDlayerCount - 1) {
       pDqLayer->pFeatureSearchPreparation = static_cast<SFeatureSearchPreparation*> (pMa->WelsMallocz (sizeof (
                                               SFeatureSearchPreparation), "pFeatureSearchPreparation"));
-      WELS_VERIFY_RETURN_PROC_IF (1, NULL == pDqLayer->pFeatureSearchPreparation, FreeMemorySvc (ppCtx));
+      WELS_VERIFY_RETURN_IF (1, NULL == pDqLayer->pFeatureSearchPreparation)
       int32_t iReturn = RequestFeatureSearchPreparation (pMa, pDlayer->iVideoWidth, pDlayer->iVideoHeight,
                         kiNeedFeatureStorage,
                         pDqLayer->pFeatureSearchPreparation);
-      WELS_VERIFY_RETURN_PROC_IF (1, ENC_RETURN_SUCCESS != iReturn, FreeMemorySvc (ppCtx));
+      WELS_VERIFY_RETURN_IF (1, ENC_RETURN_SUCCESS != iReturn)
     } else {
       pDqLayer->pFeatureSearchPreparation = NULL;
     }
@@ -1270,15 +1270,15 @@
   }
 
   // for dynamically malloc for parameter sets memory instead of maximal items for standard to reduce size, 3/18/2010
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pFuncList), FreeMemorySvc (ppCtx))
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pFuncList->pParametersetStrategy), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pFuncList))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pFuncList->pParametersetStrategy))
   const int32_t kiNeededSpsNum = (*ppCtx)->pFuncList->pParametersetStrategy->GetNeededSpsNum();
   const int32_t kiNeededSubsetSpsNum = (*ppCtx)->pFuncList->pParametersetStrategy->GetNeededSubsetSpsNum();
   (*ppCtx)->pSpsArray = (SWelsSPS*)pMa->WelsMallocz (kiNeededSpsNum * sizeof (SWelsSPS), "pSpsArray");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pSpsArray), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pSpsArray))
   if (kiNeededSubsetSpsNum > 0) {
     (*ppCtx)->pSubsetArray = (SSubsetSps*)pMa->WelsMallocz (kiNeededSubsetSpsNum * sizeof (SSubsetSps), "pSubsetArray");
-    WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pSubsetArray), FreeMemorySvc (ppCtx))
+    WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pSubsetArray))
   } else {
     (*ppCtx)->pSubsetArray = NULL;
   }
@@ -1286,7 +1286,7 @@
   // PPS
   const int32_t kiNeededPpsNum = (*ppCtx)->pFuncList->pParametersetStrategy->GetNeededPpsNum();
   (*ppCtx)->pPPSArray = (SWelsPPS*)pMa->WelsMallocz (kiNeededPpsNum * sizeof (SWelsPPS), "pPPSArray");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pPPSArray), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pPPSArray))
 
   (*ppCtx)->pFuncList->pParametersetStrategy->LoadPrevious (pExistingParasetList, (*ppCtx)->pSpsArray,
       (*ppCtx)->pSubsetArray, (*ppCtx)->pPPSArray);
@@ -1293,7 +1293,7 @@
 
 
   (*ppCtx)->pDqIdcMap = (SDqIdc*)pMa->WelsMallocz (iDlayerCount * sizeof (SDqIdc), "pDqIdcMap");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pDqIdcMap), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pDqIdcMap))
 
   iDlayerIndex = 0;
   while (iDlayerIndex < iDlayerCount) {
@@ -1328,7 +1328,6 @@
                                   pPps);
       if (iResult) {
         WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_WARNING, "InitDqLayers(), InitSlicePEncCtx failed(%d)!", iResult);
-        FreeMemorySvc (ppCtx);
         return iResult;
       }
     }
@@ -1682,7 +1681,6 @@
   if (kiNumDependencyLayers < 1 || kiNumDependencyLayers > MAX_DEPENDENCY_LAYER) {
     WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_WARNING, "RequestMemorySvc() failed due to invalid iNumDependencyLayers(%d)!",
              kiNumDependencyLayers);
-    FreeMemorySvc (ppCtx);
     return 1;
   }
 
@@ -1690,7 +1688,6 @@
     WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_WARNING,
              "RequestMemorySvc() failed due to invalid uiIntraPeriod(%d) (=multipler of uiGopSize(%d)!",
              pParam->uiIntraPeriod, pParam->uiGopSize);
-    FreeMemorySvc (ppCtx);
     return 1;
   }
 
@@ -1702,7 +1699,6 @@
   iResult = AcquireLayersNals (ppCtx, pParam, &iCountLayers, &iCountNals);
   if (iResult) {
     WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_WARNING, "RequestMemorySvc(), AcquireLayersNals failed(%d)!", iResult);
-    FreeMemorySvc (ppCtx);
     return 1;
   }
 
@@ -1755,21 +1751,20 @@
 
   // Output
   (*ppCtx)->pOut = (SWelsEncoderOutput*)pMa->WelsMallocz (sizeof (SWelsEncoderOutput), "SWelsEncoderOutput");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pOut), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pOut))
   (*ppCtx)->pOut->pBsBuffer = (uint8_t*)pMa->WelsMallocz (iCountBsLen, "pOut->pBsBuffer");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pOut->pBsBuffer), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pOut->pBsBuffer))
   (*ppCtx)->pOut->uiSize = iCountBsLen;
   (*ppCtx)->pOut->sNalList = (SWelsNalRaw*)pMa->WelsMallocz (iCountNals * sizeof (SWelsNalRaw), "pOut->sNalList");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pOut->sNalList), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pOut->sNalList))
   (*ppCtx)->pOut->pNalLen = (int32_t*)pMa->WelsMallocz (iCountNals * sizeof (int32_t), "pOut->pNalLen");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pOut->pNalLen), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pOut->pNalLen))
   (*ppCtx)->pOut->iCountNals    = iCountNals;
   (*ppCtx)->pOut->iNalIndex     = 0;
   (*ppCtx)->pOut->iLayerBsIndex = 0;
 
-
   (*ppCtx)->pFrameBs = (uint8_t*)pMa->WelsMalloc (iTotalLength, "pFrameBs");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pFrameBs), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pFrameBs))
   (*ppCtx)->iFrameBsSize = iTotalLength;
   (*ppCtx)->iPosBsBuffer = 0;
 
@@ -1777,7 +1772,7 @@
   if (bDynamicSlice && pParam->iEntropyCodingModeFlag) {
     for (int32_t iIdx = 0; iIdx < MAX_THREADS_NUM; iIdx++) {
       (*ppCtx)->pDynamicBsBuffer[iIdx] = (uint8_t*)pMa->WelsMalloc (iMaxSliceBufferSize, "DynamicSliceBs");
-      WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pDynamicBsBuffer[iIdx]), FreeMemorySvc (ppCtx))
+      WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pDynamicBsBuffer[iIdx]))
     }
   }
   // for pSlice bs buffers
@@ -1784,38 +1779,37 @@
   if (pParam->iMultipleThreadIdc > 1
       && RequestMtResource (ppCtx, pParam, iCountBsLen, iMaxSliceBufferSize, bDynamicSlice)) {
     WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_WARNING, "RequestMemorySvc(), RequestMtResource failed!");
-    FreeMemorySvc (ppCtx);
     return 1;
   }
 
   (*ppCtx)->pReferenceStrategy = IWelsReferenceStrategy::CreateReferenceStrategy ((*ppCtx), pParam->iUsageType,
                                  pParam->bEnableLongTermReference);
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pReferenceStrategy), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pReferenceStrategy))
 
   (*ppCtx)->pIntra4x4PredModeBlocks = static_cast<int8_t*>
                                       (pMa->WelsMallocz (iCountMaxMbNum * INTRA_4x4_MODE_NUM, "pIntra4x4PredModeBlocks"));
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pIntra4x4PredModeBlocks), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pIntra4x4PredModeBlocks))
 
   (*ppCtx)->pNonZeroCountBlocks = static_cast<int8_t*>
                                   (pMa->WelsMallocz (iCountMaxMbNum * MB_LUMA_CHROMA_BLOCK4x4_NUM, "pNonZeroCountBlocks"));
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pNonZeroCountBlocks), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pNonZeroCountBlocks))
 
   (*ppCtx)->pMvUnitBlock4x4 = static_cast<SMVUnitXY*>
                               (pMa->WelsMallocz (iCountMaxMbNum * 2 * MB_BLOCK4x4_NUM * sizeof (SMVUnitXY), "pMvUnitBlock4x4"));
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pMvUnitBlock4x4), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pMvUnitBlock4x4))
 
   (*ppCtx)->pRefIndexBlock4x4 = static_cast<int8_t*>
                                 (pMa->WelsMallocz (iCountMaxMbNum * 2 * MB_BLOCK8x8_NUM * sizeof (int8_t), "pRefIndexBlock4x4"));
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pRefIndexBlock4x4), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pRefIndexBlock4x4))
 
   (*ppCtx)->pSadCostMb = static_cast<int32_t*>
                          (pMa->WelsMallocz (iCountMaxMbNum * sizeof (int32_t), "pSadCostMb"));
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pSadCostMb), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pSadCostMb))
 
   (*ppCtx)->iGlobalQp = 26;   // global qp in default
 
   (*ppCtx)->pLtr = (SLTRState*)pMa->WelsMallocz (kiNumDependencyLayers * sizeof (SLTRState), "SLTRState");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pLtr), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pLtr))
   int32_t i = 0;
   for (i = 0; i < kiNumDependencyLayers; i++) {
     ResetLtrState (& (*ppCtx)->pLtr[i]);
@@ -1824,7 +1818,6 @@
   // stride tables
   if (AllocStrideTables (ppCtx, kiNumDependencyLayers)) {
     WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_WARNING, "RequestMemorySvc(), AllocStrideTables failed!");
-    FreeMemorySvc (ppCtx);
     return 1;
   }
 
@@ -1831,57 +1824,55 @@
   //Rate control module memory allocation
   // only malloc once for RC pData, 12/14/2009
   (*ppCtx)->pWelsSvcRc = (SWelsSvcRc*)pMa->WelsMallocz (kiNumDependencyLayers * sizeof (SWelsSvcRc), "pWelsSvcRc");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pWelsSvcRc), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pWelsSvcRc))
   //End of Rate control module memory allocation
 
   //pVaa memory allocation
   if (pParam->iUsageType == SCREEN_CONTENT_REAL_TIME) {
     (*ppCtx)->pVaa = (SVAAFrameInfoExt*)pMa->WelsMallocz (sizeof (SVAAFrameInfoExt), "pVaa");
-    WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pVaa), FreeMemorySvc (ppCtx))
+    WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pVaa))
     if (RequestMemoryVaaScreen ((*ppCtx)->pVaa, pMa, (*ppCtx)->pSvcParam->iMaxNumRefFrame, iCountMaxMbNum << 2)) {
       WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_WARNING, "RequestMemorySvc(), RequestMemoryVaaScreen failed!");
-      FreeMemorySvc (ppCtx);
       return 1;
     }
   } else {
     (*ppCtx)->pVaa = (SVAAFrameInfo*)pMa->WelsMallocz (sizeof (SVAAFrameInfo), "pVaa");
-    WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pVaa), FreeMemorySvc (ppCtx))
+    WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pVaa))
   }
 
   if ((*ppCtx)->pSvcParam->bEnableAdaptiveQuant) { //malloc mem
     (*ppCtx)->pVaa->sAdaptiveQuantParam.pMotionTextureUnit   = static_cast<SMotionTextureUnit*>
         (pMa->WelsMallocz (iCountMaxMbNum * sizeof (SMotionTextureUnit), "pVaa->sAdaptiveQuantParam.pMotionTextureUnit"));
-    WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pVaa->sAdaptiveQuantParam.pMotionTextureUnit), FreeMemorySvc (ppCtx))
+    WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pVaa->sAdaptiveQuantParam.pMotionTextureUnit))
     (*ppCtx)->pVaa->sAdaptiveQuantParam.pMotionTextureIndexToDeltaQp   = static_cast<int8_t*>
         (pMa->WelsMallocz (iCountMaxMbNum * sizeof (int8_t), "pVaa->sAdaptiveQuantParam.pMotionTextureIndexToDeltaQp"));
-    WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pVaa->sAdaptiveQuantParam.pMotionTextureIndexToDeltaQp),
-                                FreeMemorySvc (ppCtx))
+    WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pVaa->sAdaptiveQuantParam.pMotionTextureIndexToDeltaQp))
   }
 
   (*ppCtx)->pVaa->pVaaBackgroundMbFlag = (int8_t*)pMa->WelsMallocz (iCountMaxMbNum * sizeof (int8_t),
                                          "pVaa->pVaaBackgroundMbFlag");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pVaa->pVaaBackgroundMbFlag), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pVaa->pVaaBackgroundMbFlag))
 
   (*ppCtx)->pVaa->sVaaCalcInfo.pSad8x8 = static_cast<int32_t (*)[4]>
                                          (pMa->WelsMallocz (iCountMaxMbNum * 4 * sizeof (int32_t), "pVaa->sVaaCalcInfo.sad8x8"));
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pVaa->sVaaCalcInfo.pSad8x8), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pVaa->sVaaCalcInfo.pSad8x8))
   (*ppCtx)->pVaa->sVaaCalcInfo.pSsd16x16 = static_cast<int32_t*>
       (pMa->WelsMallocz (iCountMaxMbNum * sizeof (int32_t), "pVaa->sVaaCalcInfo.pSsd16x16"));
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pVaa->sVaaCalcInfo.pSsd16x16), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pVaa->sVaaCalcInfo.pSsd16x16))
   (*ppCtx)->pVaa->sVaaCalcInfo.pSum16x16 = static_cast<int32_t*>
       (pMa->WelsMallocz (iCountMaxMbNum * sizeof (int32_t), "pVaa->sVaaCalcInfo.pSum16x16"));
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pVaa->sVaaCalcInfo.pSum16x16), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pVaa->sVaaCalcInfo.pSum16x16))
   (*ppCtx)->pVaa->sVaaCalcInfo.pSumOfSquare16x16 = static_cast<int32_t*>
       (pMa->WelsMallocz (iCountMaxMbNum * sizeof (int32_t), "pVaa->sVaaCalcInfo.pSumOfSquare16x16"));
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pVaa->sVaaCalcInfo.pSumOfSquare16x16), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pVaa->sVaaCalcInfo.pSumOfSquare16x16))
 
   if ((*ppCtx)->pSvcParam->bEnableBackgroundDetection) { //BGD control
     (*ppCtx)->pVaa->sVaaCalcInfo.pSumOfDiff8x8 = static_cast<int32_t (*)[4]>
         (pMa->WelsMallocz (iCountMaxMbNum * 4 * sizeof (int32_t), "pVaa->sVaaCalcInfo.pSumOfDiff8x8"));
-    WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pVaa->sVaaCalcInfo.pSumOfDiff8x8), FreeMemorySvc (ppCtx))
+    WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pVaa->sVaaCalcInfo.pSumOfDiff8x8))
     (*ppCtx)->pVaa->sVaaCalcInfo.pMad8x8 = static_cast<uint8_t (*)[4]>
                                            (pMa->WelsMallocz (iCountMaxMbNum * 4 * sizeof (uint8_t), "pVaa->sVaaCalcInfo.pMad8x8"));
-    WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pVaa->sVaaCalcInfo.pMad8x8), FreeMemorySvc (ppCtx))
+    WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pVaa->sVaaCalcInfo.pMad8x8))
   }
 
   //End of pVaa memory allocation
@@ -1888,21 +1879,19 @@
 
   (*ppCtx)->ppRefPicListExt = (SRefList**)pMa->WelsMallocz (kiNumDependencyLayers * sizeof (SRefList*),
                               "ppRefPicListExt");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->ppRefPicListExt), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->ppRefPicListExt))
 
   (*ppCtx)->ppDqLayerList = (SDqLayer**)pMa->WelsMallocz (kiNumDependencyLayers * sizeof (SDqLayer*), "ppDqLayerList");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->ppDqLayerList), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->ppDqLayerList))
 
   iResult = InitDqLayers (ppCtx, pExistingParasetList);
   if (iResult) {
     WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_WARNING, "RequestMemorySvc(), InitDqLayers failed(%d)!", iResult);
-    FreeMemorySvc (ppCtx);
     return iResult;
   }
 
   if (InitMbListD (ppCtx)) {
     WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_WARNING, "RequestMemorySvc(), InitMbListD failed!");
-    FreeMemorySvc (ppCtx);
     return 1;
   }
 
@@ -1915,7 +1904,7 @@
   (*ppCtx)->iMvdCostTableSize = kuiMvdInterTableSize;
   (*ppCtx)->iMvdCostTableStride = kuiMvdInterTableStride;
   (*ppCtx)->pMvdCostTable = (uint16_t*)pMa->WelsMallocz (52 * kuiMvdCacheAlignedSize, "pMvdCostTable");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pMvdCostTable), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pMvdCostTable))
   MvdCostInit ((*ppCtx)->pMvdCostTable, kuiMvdInterTableStride);  //should put to a better place?
 
   if ((*ppCtx)->ppRefPicListExt[0] != NULL && (*ppCtx)->ppRefPicListExt[0]->pRef[0] != NULL)
--- a/codec/encoder/core/src/slice_multi_threading.cpp
+++ b/codec/encoder/core/src/slice_multi_threading.cpp
@@ -87,7 +87,7 @@
   const int32_t kiEndMbInSlice   = iIdx + pUpdateSlice->iCountMbNumInSlice - 1;
 
   do {
-    UpdateMbNeighbor(pCurDq, &pMbList[iIdx], kiMbWidth, uiSliceIdc);
+    UpdateMbNeighbor (pCurDq, &pMbList[iIdx], kiMbWidth, uiSliceIdc);
     ++ iIdx;
   } while (iIdx <= kiEndMbInSlice);
 }
@@ -118,7 +118,7 @@
 }
 
 int32_t NeedDynamicAdjust (SSlice* pSliceInLayer, const int32_t iSliceNum) {
-  if ( NULL == pSliceInLayer )
+  if (NULL == pSliceInLayer)
     return false;
 
   uint32_t uiTotalConsume       = 0;
@@ -207,7 +207,8 @@
 
   WelsEmms();
 
-  MT_TRACE_LOG (&(pCtx->sLogCtx), WELS_LOG_DEBUG, "[MT] DynamicAdjustSlicing(), iDid= %d, iCountNumMb= %d", iCurDid, kiCountNumMb);
+  MT_TRACE_LOG (& (pCtx->sLogCtx), WELS_LOG_DEBUG, "[MT] DynamicAdjustSlicing(), iDid= %d, iCountNumMb= %d", iCurDid,
+                kiCountNumMb);
 
   iSliceIdx = 0;
   while (iSliceIdx + 1 < kiCountSliceNum) {
@@ -232,7 +233,7 @@
       return;
     }
     iRunLen[iSliceIdx] = iNumMbAssigning;
-    MT_TRACE_LOG (&(pCtx->sLogCtx), WELS_LOG_DEBUG,
+    MT_TRACE_LOG (& (pCtx->sLogCtx), WELS_LOG_DEBUG,
                   "[MT] DynamicAdjustSlicing(), uiSliceIdx= %d, iSliceComplexRatio= %.2f, slice_run_org= %d, slice_run_adj= %d",
                   iSliceIdx, pSliceInLayer[iSliceIdx].iSliceComplexRatio * 1.0f / INT_MULTIPLY,
                   pSliceInLayer[iSliceIdx].iCountMbNumInSlice,
@@ -241,9 +242,10 @@
     iMaximalMbNum = iMbNumLeft - (kiCountSliceNum - iSliceIdx - 1) * iMinimalMbNum; // get maximal num_mb in left parts
   }
   iRunLen[iSliceIdx] = iMbNumLeft;
-  MT_TRACE_LOG (&(pCtx->sLogCtx), WELS_LOG_DEBUG,
+  MT_TRACE_LOG (& (pCtx->sLogCtx), WELS_LOG_DEBUG,
                 "[MT] DynamicAdjustSlicing(), iSliceIdx= %d, pSliceComplexRatio= %.2f, slice_run_org= %d, slice_run_adj= %d",
-                iSliceIdx, pSliceInLayer[iSliceIdx].iSliceComplexRatio * 1.0f / INT_MULTIPLY, pSliceInLayer[iSliceIdx].iCountMbNumInSlice, iMbNumLeft);
+                iSliceIdx, pSliceInLayer[iSliceIdx].iSliceComplexRatio * 1.0f / INT_MULTIPLY,
+                pSliceInLayer[iSliceIdx].iCountMbNumInSlice, iMbNumLeft);
 
   pCurDqLayer->bNeedAdjustingSlicing = !DynamicAdjustSlicePEncCtxAll (pCurDqLayer, iRunLen);
 }
@@ -274,11 +276,12 @@
   iMaxSliceNumInThread =  WELS_MIN ((*ppCtx)->iMaxSliceCount, (int) iMaxSliceNumInThread);
 
   pSmt = (SSliceThreading*)pMa->WelsMalloc (sizeof (SSliceThreading), "SSliceThreading");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == pSmt))
+  memset (pSmt, 0, sizeof (SSliceThreading));
   (*ppCtx)->pSliceThreading = pSmt;
   pSmt->pThreadPEncCtx = (SSliceThreadPrivateData*)pMa->WelsMalloc (sizeof (SSliceThreadPrivateData) * iThreadNum,
                          "pThreadPEncCtx");
-  WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pThreadPEncCtx), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == pSmt->pThreadPEncCtx))
 
 #ifdef _WIN32
   // Dummy event namespace, the windows events don't actually use this
@@ -333,9 +336,6 @@
                   (void*)pSmt->pReadySliceCodingEvent[iIdx], name, err, errno);
     ++ iIdx;
   }
-  for (; iIdx < MAX_THREADS_NUM; iIdx++) {
-    pSmt->pThreadBsBuffer[iIdx]      = NULL;
-  }
 
   WelsSnprintf (name, SEM_NAME_MAX, "scm%s", pSmt->eventNamespace);
   err = WelsEventOpen (&pSmt->pSliceCodedMasterEvent, name);
@@ -342,29 +342,23 @@
   MT_TRACE_LOG (pLogCtx, WELS_LOG_INFO, "[MT] Open pSliceCodedMasterEvent named(%s) ret%d err%d", name, err, errno);
 
   iReturn = WelsMutexInit (&pSmt->mutexSliceNumUpdate);
-  WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (WELS_THREAD_ERROR_OK != iReturn))
 
   (*ppCtx)->pTaskManage = IWelsTaskManage::CreateTaskManage (*ppCtx, iNumSpatialLayers, bDynamicSlice);
-  WELS_VERIFY_RETURN_PROC_IF (iReturn, (NULL == (*ppCtx)->pTaskManage), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pTaskManage))
 
-  int32_t iThreadBufferNum = WELS_MIN((*ppCtx)->pTaskManage->GetThreadPoolThreadNum(), MAX_THREADS_NUM);
-  for (iIdx = 0;iIdx < iThreadBufferNum; iIdx++) {
+  int32_t iThreadBufferNum = WELS_MIN ((*ppCtx)->pTaskManage->GetThreadPoolThreadNum(), MAX_THREADS_NUM);
+
+  for (iIdx = 0; iIdx < iThreadBufferNum; iIdx++) {
     pSmt->pThreadBsBuffer[iIdx] = (uint8_t*)pMa->WelsMalloc (iCountBsLen, "pSmt->pThreadBsBuffer");
-    WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pThreadBsBuffer[iIdx]), FreeMemorySvc (ppCtx))
+    WELS_VERIFY_RETURN_IF (1, (NULL == pSmt->pThreadBsBuffer[iIdx]))
   }
-  if (iThreadBufferNum < MAX_THREADS_NUM) {
-    for (iIdx = iThreadBufferNum; iIdx < MAX_THREADS_NUM; iIdx++) {
-      pSmt->pThreadBsBuffer[iIdx] = NULL;
-    }
-  }
-
-  memset (&pSmt->bThreadBsBufferUsage, 0, MAX_THREADS_NUM * sizeof (bool));
   iReturn = WelsMutexInit (&pSmt->mutexThreadBsBufferUsage);
-  WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (WELS_THREAD_ERROR_OK != iReturn))
   iReturn = WelsMutexInit (&pSmt->mutexEvent);
-  WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx));
+  WELS_VERIFY_RETURN_IF (1, (WELS_THREAD_ERROR_OK != iReturn))
   iReturn = WelsMutexInit (& (*ppCtx)->mutexEncoderError);
-  WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx))
+  WELS_VERIFY_RETURN_IF (1, (WELS_THREAD_ERROR_OK != iReturn))
 
   MT_TRACE_LOG (pLogCtx, WELS_LOG_INFO, "RequestMtResource(), iThreadNum=%d, iMultipleThreadIdc= %d",
                 pPara->iMultipleThreadIdc,
@@ -427,7 +421,7 @@
   memset (&pSmt->bThreadBsBufferUsage, 0, MAX_THREADS_NUM * sizeof (bool));
 
   if ((*ppCtx)->pTaskManage != NULL) {
-    WELS_DELETE_OP((*ppCtx)->pTaskManage);
+    WELS_DELETE_OP ((*ppCtx)->pTaskManage);
   }
 
 #ifdef MT_DEBUG
@@ -516,7 +510,7 @@
   return iLayerSize;
 }
 
-int32_t WriteSliceBs (sWelsEncCtx* pCtx,SWelsSliceBs* pSliceBs,const int32_t iSliceIdx,int32_t& iSliceSize) {
+int32_t WriteSliceBs (sWelsEncCtx* pCtx, SWelsSliceBs* pSliceBs, const int32_t iSliceIdx, int32_t& iSliceSize) {
   const int32_t kiNalCnt        = pSliceBs->iNalIndex;
   int32_t iNalIdx               = 0;
   int32_t iNalSize              = 0;
@@ -581,12 +575,13 @@
   WelsThreadSetName ("OpenH264Enc_CodingSliceThreadProc");
 
   do {
-    MT_TRACE_LOG (&(pEncPEncCtx->sLogCtx), WELS_LOG_INFO,
+    MT_TRACE_LOG (& (pEncPEncCtx->sLogCtx), WELS_LOG_INFO,
                   "[MT] CodingSliceThreadProc(), try to call WelsMultipleEventsWaitSingleBlocking(pEventsList= %p %p %p), pEncPEncCtx= %p!",
                   pEventsList[0], pEventsList[1], pEventsList[1], (void*)pEncPEncCtx);
     iWaitRet = WelsMultipleEventsWaitSingleBlocking (iEventCount,
                &pEventsList[0],
-               &pEncPEncCtx->pSliceThreading->pThreadMasterEvent[iEventIdx],&pEncPEncCtx->pSliceThreading->mutexEvent); // blocking until at least one event is signalled
+               &pEncPEncCtx->pSliceThreading->pThreadMasterEvent[iEventIdx],
+               &pEncPEncCtx->pSliceThreading->mutexEvent); // blocking until at least one event is signalled
     if (WELS_THREAD_ERROR_WAIT_OBJECT_0 == iWaitRet) { // start pSlice coding signal waited
       //int             iLayerIndex  = pEncPEncCtx->pOut->iLayerBsIndex;
       //SFrameBSInfo*   pFrameBsInfo = pPrivateData->pFrameBsInfo;
@@ -658,7 +653,7 @@
         pEncPEncCtx->pFuncList->pfDeblocking.pfDeblockingFilterSlice (pCurDq, pEncPEncCtx->pFuncList, iSliceIdx);
 
         if (bDsaFlag) {
-            pEncPEncCtx->pCurDqLayer->sLayerInfo.pSliceInLayer[iSliceIdx].uiSliceConsumeTime = (uint32_t) (
+          pEncPEncCtx->pCurDqLayer->sLayerInfo.pSliceInLayer[iSliceIdx].uiSliceConsumeTime = (uint32_t) (
                 WelsTime() - iSliceStart);
           MT_TRACE_LOG (& (pEncPEncCtx->sLogCtx), WELS_LOG_INFO,
                         "[MT] CodingSliceThreadProc(), coding_idx %d, uiSliceIdx %d, uiSliceConsumeTime %d, iSliceSize %d, iFirstMbInSlice %d, count_num_mb_in_slice %d",
@@ -693,7 +688,7 @@
         const int32_t kiFirstMbInPartition      = pPrivateData->iStartMbIndex;  // inclusive
         const int32_t kiEndMbInPartition        = pPrivateData->iEndMbIndex;            // exclusive
         int32_t iAnyMbLeftInPartition           = kiEndMbInPartition - kiFirstMbInPartition;
-        SSpatialLayerInternal *pParamInternal = &pCodingParam->sDependencyLayers[kiCurDid];
+        SSpatialLayerInternal* pParamInternal = &pCodingParam->sDependencyLayers[kiCurDid];
         iSliceIdx = pPrivateData->iSliceIndex;
         SSliceHeaderExt* pStartSliceHeaderExt                   = &pCurDq->sLayerInfo.pSliceInLayer[iSliceIdx].sSliceHeaderExt;
         pStartSliceHeaderExt->sSliceHeader.iFirstMbInSlice      = kiFirstMbInPartition;
@@ -774,7 +769,7 @@
                   );
 #endif//SLICE_INFO_OUTPUT
 
-          MT_TRACE_LOG (&(pEncPEncCtx->sLogCtx), WELS_LOG_INFO,
+          MT_TRACE_LOG (& (pEncPEncCtx->sLogCtx), WELS_LOG_INFO,
                         "[MT] CodingSliceThreadProc(), coding_idx %d, iPartitionId %d, uiSliceIdx %d, iSliceSize %d, count_mb_slice %d, iEndMbInPartition %d, pCurDq->pLastCodedMbIdxOfPartition[%d] %d\n",
                         pEncPEncCtx->iCodingIndex, kiPartitionId, iSliceIdx, iSliceSize,
                         pCurDq->sLayerInfo.pSliceInLayer[iSliceIdx].iCountMbNumInSlice,
@@ -833,7 +828,7 @@
 
     ++ iIdx;
   }
-  MT_TRACE_LOG (&(pCtx->sLogCtx), WELS_LOG_INFO, "CreateSliceThreads() exit..");
+  MT_TRACE_LOG (& (pCtx->sLogCtx), WELS_LOG_INFO, "CreateSliceThreads() exit..");
   return 0;
 }
 
@@ -1007,7 +1002,7 @@
         int32_t iMaxI = 0;
         while (i < kuiCountSliceNum) {
           fprintf (pCtx->pSliceThreading->pFSliceDiff, "%6d us consume_time coding_idx %d iDid %d pSlice %d\n",
-                     pSliceInLayer[i].uiSliceConsumeTime, pCtx->iCodingIndex, kiDid, i /*/ 1000*/);
+                   pSliceInLayer[i].uiSliceConsumeTime, pCtx->iCodingIndex, kiDid, i /*/ 1000*/);
           if (pSliceInLayer[i].uiSliceConsumeTime > uiMaxT) {
             uiMaxT = pSliceInLayer[i].uiSliceConsumeTime;
             iMaxI = i;
--- a/codec/encoder/core/src/wels_task_management.cpp
+++ b/codec/encoder/core/src/wels_task_management.cpp
@@ -65,9 +65,9 @@
   pTaskManage = WELS_NEW_OP (CWelsTaskManageBase(), CWelsTaskManageBase);
   WELS_VERIFY_RETURN_IF (NULL, NULL == pTaskManage)
 
-  if ( ENC_RETURN_SUCCESS != pTaskManage->Init (pCtx) ) {
+  if (ENC_RETURN_SUCCESS != pTaskManage->Init (pCtx)) {
     pTaskManage->Uninit();
-    WELS_DELETE_OP(pTaskManage);
+    WELS_DELETE_OP (pTaskManage);
   }
   return pTaskManage;
 }
@@ -85,7 +85,7 @@
   }
 
   WelsEventOpen (&m_hTaskEvent);
-  WelsMutexInit(&m_hEventMutex);
+  WelsMutexInit (&m_hEventMutex);
 }
 
 CWelsTaskManageBase::~CWelsTaskManageBase() {
@@ -100,8 +100,8 @@
   int32_t iReturn = ENC_RETURN_SUCCESS;
   //fprintf(stdout, "m_pThreadPool = &(CWelsThreadPool::GetInstance, this=%x\n", this);
   iReturn = CWelsThreadPool::SetThreadNum (m_iThreadNum);
-  m_pThreadPool = & (CWelsThreadPool::AddReference ());
-  if ( (iReturn != ENC_RETURN_SUCCESS) && pEncCtx ) {
+  m_pThreadPool = (CWelsThreadPool::AddReference());
+  if ((iReturn != ENC_RETURN_SUCCESS) && pEncCtx) {
     WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_WARNING, "Set Thread Num to %d did not succeed, current thread num in use: %d",
              m_iThreadNum, m_pThreadPool->GetThreadNum());
   }
@@ -122,17 +122,18 @@
 void   CWelsTaskManageBase::Uninit() {
   DestroyTasks();
   //fprintf(stdout, "m_pThreadPool = m_pThreadPool->RemoveInstance\n");
-  m_pThreadPool->RemoveInstance();
+  if (m_pThreadPool)
+    m_pThreadPool->RemoveInstance();
   //WELS_DELETE_OP (m_pThreadPool);
 
   //fprintf(stdout, "m_pThreadPool = m_pThreadPool->RemoveInstance2\n");
 
   for (int32_t iDid = 0; iDid < MAX_DEPENDENCY_LAYER; iDid++) {
-    WELS_DELETE_OP(m_cEncodingTaskList[iDid]);
-    WELS_DELETE_OP(m_cPreEncodingTaskList[iDid]);
+    WELS_DELETE_OP (m_cEncodingTaskList[iDid]);
+    WELS_DELETE_OP (m_cPreEncodingTaskList[iDid]);
   }
   WelsEventClose (&m_hTaskEvent);
-  WelsMutexDestroy(&m_hEventMutex);
+  WelsMutexDestroy (&m_hEventMutex);
 }
 
 WelsErrorType CWelsTaskManageBase::CreateTasks (sWelsEncCtx* pEncCtx, const int32_t kiCurDid) {
@@ -164,7 +165,7 @@
       }
     }
     WELS_VERIFY_RETURN_IF (ENC_RETURN_MEMALLOCERR, NULL == pTask)
-    WELS_VERIFY_RETURN_IF (ENC_RETURN_MEMALLOCERR, true != m_cEncodingTaskList[kiCurDid]->push_back (pTask) );
+    WELS_VERIFY_RETURN_IF (ENC_RETURN_MEMALLOCERR, true != m_cEncodingTaskList[kiCurDid]->push_back (pTask));
   }
 
   //fprintf(stdout, "CWelsTaskManageBase CreateTasks m_iThreadNum %d kiTaskCount=%d\n", m_iThreadNum, kiTaskCount);
@@ -227,7 +228,7 @@
     m_pThreadPool->QueueTask (pTargetTaskList->getNode (iIdx));
     iIdx ++;
   }
-  WelsEventWait (&m_hTaskEvent,&m_hEventMutex);
+  WelsEventWait (&m_hTaskEvent, &m_hEventMutex);
 
   return ENC_RETURN_SUCCESS;
 }
--- a/codec/encoder/plus/src/welsEncoderExt.cpp
+++ b/codec/encoder/plus/src/welsEncoderExt.cpp
@@ -382,8 +382,6 @@
   const int32_t kiEncoderReturn = EncodeFrameInternal (kpSrcPic, pBsInfo);
 
   if (kiEncoderReturn != cmResultSuccess) {
-    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_DEBUG,
-             "CWelsH264SVCEncoder::EncodeFrame() not succeed, err=%d", kiEncoderReturn);
     return kiEncoderReturn;
   }
 
@@ -404,6 +402,8 @@
 
   if ((kiEncoderReturn == ENC_RETURN_MEMALLOCERR) || (kiEncoderReturn == ENC_RETURN_MEMOVERFLOWFOUND)
       || (kiEncoderReturn == ENC_RETURN_VLCOVERFLOWFOUND)) {
+    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_DEBUG, "CWelsH264SVCEncoder::EncodeFrame() not succeed, err=%d",
+             kiEncoderReturn);
     WelsUninitEncoderExt (&m_pEncContext);
     return cmMallocMemeError;
   } else if ((kiEncoderReturn != ENC_RETURN_SUCCESS) && (kiEncoderReturn == ENC_RETURN_CORRECTED)) {
--- a/test/api/decode_api_test.cpp
+++ b/test/api/decode_api_test.cpp
@@ -27,7 +27,7 @@
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
   decoder_->SetOption (DECODER_OPTION_TRACE_LEVEL, &iTraceLevel);
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
 
   int iIdx = 0;
   while (iIdx <= p.numframes) {
@@ -63,7 +63,7 @@
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
   decoder_->SetOption (DECODER_OPTION_TRACE_LEVEL, &iTraceLevel);
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
 
   int32_t iEncFrameNum = -1;
   int32_t iDecFrameNum;
@@ -102,7 +102,7 @@
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
   decoder_->SetOption (DECODER_OPTION_TRACE_LEVEL, &iTraceLevel);
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
 
   int32_t iEncCurIdrPicId = 0;
   int32_t iDecCurIdrPicId;
@@ -146,7 +146,7 @@
   int rv = encoder_->InitializeExt (&param_);
   ASSERT_TRUE (rv == cmResultSuccess);
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
   int32_t iTraceLevel = WELS_LOG_QUIET;
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
   decoder_->SetOption (DECODER_OPTION_TRACE_LEVEL, &iTraceLevel);
@@ -200,7 +200,7 @@
   int rv = encoder_->InitializeExt (&param_);
   ASSERT_TRUE (rv == cmResultSuccess);
   m_LTR_Recover_Request.uiFeedbackType = NO_RECOVERY_REQUSET;
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
   int32_t iTraceLevel = WELS_LOG_QUIET;
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
   decoder_->SetOption (DECODER_OPTION_TRACE_LEVEL, &iTraceLevel);
@@ -273,7 +273,7 @@
   ASSERT_TRUE (rv == cmResultSuccess);
   m_LTR_Recover_Request.uiFeedbackType = NO_RECOVERY_REQUSET;
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
   int32_t iTraceLevel = WELS_LOG_QUIET;
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
   decoder_->SetOption (DECODER_OPTION_TRACE_LEVEL, &iTraceLevel);
@@ -329,7 +329,7 @@
   ASSERT_TRUE (rv == cmResultSuccess);
   m_LTR_Recover_Request.uiFeedbackType = NO_RECOVERY_REQUSET;
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
   int32_t iTraceLevel = WELS_LOG_QUIET;
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
   decoder_->SetOption (DECODER_OPTION_TRACE_LEVEL, &iTraceLevel);
@@ -386,7 +386,7 @@
   ASSERT_TRUE (rv == cmResultSuccess);
   m_LTR_Recover_Request.uiFeedbackType = NO_RECOVERY_REQUSET;
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
   int32_t iTraceLevel = WELS_LOG_QUIET;
   pFunc = TestOutPutTrace;
   pTraceInfo = &sTrace;
@@ -451,7 +451,7 @@
   ASSERT_TRUE (rv == cmResultSuccess);
   m_LTR_Recover_Request.uiFeedbackType = NO_RECOVERY_REQUSET;
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
 
   int32_t iTraceLevel = WELS_LOG_QUIET;
   pFunc = NULL;
@@ -529,7 +529,6 @@
     EncodeDecodeTestBase::prepareParam (iLayerNum, iSliceNum,  width, height, framerate, pParam);
   }
 
-  void prepareEncDecParam (const EncodeDecodeFileParamBase EncDecFileParam);
   void EncodeOneFrame() {
     int frameSize = EncPic.iPicWidth * EncPic.iPicHeight * 3 / 2;
     memset (buf_.data(), iRandValue, (frameSize >> 2));
@@ -541,31 +540,6 @@
   unsigned char* ucBuf_;
 };
 
-void DecodeCrashTestAPI::prepareEncDecParam (const EncodeDecodeFileParamBase EncDecFileParam) {
-  // for encoder
-  // I420: 1(Y) + 1/4(U) + 1/4(V)
-  int frameSize = EncDecFileParam.width * EncDecFileParam.height * 3 / 2;
-
-  buf_.SetLength (frameSize);
-  ASSERT_TRUE (buf_.Length() == (size_t)frameSize);
-
-  memset (&EncPic, 0, sizeof (SSourcePicture));
-  EncPic.iPicWidth = EncDecFileParam.width;
-  EncPic.iPicHeight = EncDecFileParam.height;
-  EncPic.iColorFormat = videoFormatI420;
-  EncPic.iStride[0] = EncPic.iPicWidth;
-  EncPic.iStride[1] = EncPic.iStride[2] = EncPic.iPicWidth >> 1;
-  EncPic.pData[0] = buf_.data();
-  EncPic.pData[1] = EncPic.pData[0] + EncDecFileParam.width * EncDecFileParam.height;
-  EncPic.pData[2] = EncPic.pData[1] + (EncDecFileParam.width * EncDecFileParam.height >> 2);
-
-  //for decoder
-  memset (&info, 0, sizeof (SFrameBSInfo));
-
-  //set a fixed random value
-  iRandValue = rand() % 256;
-}
-
 struct EncodeDecodeParamBase {
   int width;
   int height;
@@ -668,7 +642,7 @@
     pInput.width =  p.width;
     pInput.height = p.height;
     pInput.frameRate = p.frameRate;
-    prepareEncDecParam (pInput);
+    ASSERT_TRUE (prepareEncDecParam (pInput));
     while (iIdx++ < iTotalFrameNum) { // loop in frame
       EncodeOneFrame();
 #ifdef DEBUG_FILE_SAVE_CRA
@@ -819,11 +793,13 @@
     }
   }
 
-  void prepareEncDecParam (const EncodeDecodeFileParamBase p) {
-    EncodeDecodeTestBase::prepareEncDecParam (p);
+  bool prepareEncDecParam (const EncodeDecodeFileParamBase p) {
+    if (!EncodeDecodeTestBase::prepareEncDecParam (p))
+      return false;
     unsigned char* pTmpPtr = BsInfo_.pDstBuff; //store for restore
     memset (&BsInfo_, 0, sizeof (SParserBsInfo));
     BsInfo_.pDstBuff = pTmpPtr;
+    return true;
   }
 
   void MockInputData (uint8_t* pData, int32_t iSize) {
@@ -886,7 +862,7 @@
   if (uiTargetLayerId < kiTotalLayer) { //should always be true
     //Start for enc
     int iLen = 0;
-    prepareEncDecParam (p);
+    ASSERT_TRUE (prepareEncDecParam (p));
     int iFrame = 0;
 
     while (iFrame < p.numframes) {
@@ -959,7 +935,7 @@
   //Start for enc
   int iLen = 0;
   uint32_t uiGet;
-  prepareEncDecParam (p);
+  ASSERT_TRUE (prepareEncDecParam (p));
   int iFrame = 0;
 
   while (iFrame < p.numframes) {
--- a/test/api/decoder_ec_test.cpp
+++ b/test/api/decoder_ec_test.cpp
@@ -35,7 +35,7 @@
   ASSERT_EQ (0, rv);
   m_LTR_Recover_Request.uiFeedbackType = NO_RECOVERY_REQUSET;
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
   int32_t iTraceLevel = WELS_LOG_QUIET;
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
   decoder_->SetOption (DECODER_OPTION_TRACE_LEVEL, &iTraceLevel);
@@ -91,7 +91,7 @@
   ASSERT_TRUE (rv == cmResultSuccess);
   m_LTR_Recover_Request.uiFeedbackType = NO_RECOVERY_REQUSET;
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
   int32_t iTraceLevel = WELS_LOG_QUIET;
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
   decoder_->SetOption (DECODER_OPTION_TRACE_LEVEL, &iTraceLevel);
@@ -162,7 +162,7 @@
 
   //Start for enc/dec
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
   int32_t iTraceLevel = WELS_LOG_QUIET;
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
   decoder_->SetOption (DECODER_OPTION_TRACE_LEVEL, &iTraceLevel);
@@ -249,7 +249,7 @@
   int iIdx = 0;
   int len = 0;
   unsigned char* pData[3] = { NULL };
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
   //Frame 0: IDR, EC_IDC=DISABLE, loss = 0
   EncodeOneFrame (1);
   encToDecData (info, len);
@@ -383,7 +383,7 @@
   unsigned char* pData[3] = { NULL };
   int iTotalSliceSize = 0;
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
 
   //Frame 0: IDR, EC_IDC=2, loss = 2
   EncodeOneFrame (1);
@@ -515,7 +515,7 @@
   unsigned char* pData[3] = { NULL };
   int iTotalSliceSize = 0;
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
 
   //set EC=DISABLE
   uiEcIdc = (uint32_t) (ERROR_CON_DISABLE);
@@ -677,7 +677,7 @@
   ASSERT_TRUE (rv == cmResultSuccess);
   m_LTR_Recover_Request.uiFeedbackType = NO_RECOVERY_REQUSET;
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
   int32_t iTraceLevel = WELS_LOG_QUIET;
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
   decoder_->SetOption (DECODER_OPTION_TRACE_LEVEL, &iTraceLevel);
@@ -744,7 +744,7 @@
   ASSERT_TRUE (rv == cmResultSuccess);
   m_LTR_Recover_Request.uiFeedbackType = NO_RECOVERY_REQUSET;
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
   int32_t iTraceLevel = WELS_LOG_QUIET;
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
   decoder_->SetOption (DECODER_OPTION_TRACE_LEVEL, &iTraceLevel);
--- a/test/api/encode_decode_api_test.cpp
+++ b/test/api/encode_decode_api_test.cpp
@@ -11,168 +11,183 @@
 using namespace WelsCommon;
 
 
-void EncodeDecodeTestBase::prepareParam (int iLayers, int iSlices, int width, int height, float framerate, SEncParamExt* pParam) {
-    pParam->iUsageType = CAMERA_VIDEO_REAL_TIME;
-    pParam->iPicWidth = width;
-    pParam->iPicHeight = height;
-    pParam->fMaxFrameRate = framerate;
-    pParam->iRCMode = RC_OFF_MODE; //rc off
-    pParam->iMultipleThreadIdc = 1; //single thread
-    pParam->iSpatialLayerNum = iLayers;
-    pParam->iNumRefFrame = AUTO_REF_PIC_COUNT;
-    for (int i = 0; i < iLayers; i++) {
-      pParam->sSpatialLayers[i].iVideoWidth = width >> (iLayers - i - 1);
-      pParam->sSpatialLayers[i].iVideoHeight = height >> (iLayers - i - 1);
-      pParam->sSpatialLayers[i].fFrameRate = framerate;
-      pParam->sSpatialLayers[i].sSliceArgument.uiSliceMode = SM_FIXEDSLCNUM_SLICE;
-      pParam->sSpatialLayers[i].sSliceArgument.uiSliceNum = iSlices;
-    }
+void EncodeDecodeTestBase::prepareParam (int iLayers, int iSlices, int width, int height, float framerate,
+    SEncParamExt* pParam) {
+  pParam->iUsageType = CAMERA_VIDEO_REAL_TIME;
+  pParam->iPicWidth = width;
+  pParam->iPicHeight = height;
+  pParam->fMaxFrameRate = framerate;
+  pParam->iRCMode = RC_OFF_MODE; //rc off
+  pParam->iMultipleThreadIdc = 1; //single thread
+  pParam->iSpatialLayerNum = iLayers;
+  pParam->iNumRefFrame = AUTO_REF_PIC_COUNT;
+  for (int i = 0; i < iLayers; i++) {
+    pParam->sSpatialLayers[i].iVideoWidth = width >> (iLayers - i - 1);
+    pParam->sSpatialLayers[i].iVideoHeight = height >> (iLayers - i - 1);
+    pParam->sSpatialLayers[i].fFrameRate = framerate;
+    pParam->sSpatialLayers[i].sSliceArgument.uiSliceMode = SM_FIXEDSLCNUM_SLICE;
+    pParam->sSpatialLayers[i].sSliceArgument.uiSliceNum = iSlices;
+  }
 }
 
-void EncodeDecodeTestBase::prepareEncDecParam (const EncodeDecodeFileParamBase EncDecFileParam) {
-    //for encoder
-    //I420: 1(Y) + 1/4(U) + 1/4(V)
-    int frameSize = EncDecFileParam.width * EncDecFileParam.height * 3 / 2;
-    buf_.SetLength (frameSize);
-    ASSERT_TRUE (buf_.Length() == (size_t)frameSize);
-    memset (&EncPic, 0, sizeof (SSourcePicture));
-    EncPic.iPicWidth = EncDecFileParam.width;
-    EncPic.iPicHeight = EncDecFileParam.height;
-    EncPic.iColorFormat = videoFormatI420;
-    EncPic.iStride[0] = EncPic.iPicWidth;
-    EncPic.iStride[1] = EncPic.iStride[2] = EncPic.iPicWidth >> 1;
-    EncPic.pData[0] = buf_.data();
-    EncPic.pData[1] = EncPic.pData[0] + EncDecFileParam.width * EncDecFileParam.height;
-    EncPic.pData[2] = EncPic.pData[1] + (EncDecFileParam.width * EncDecFileParam.height >> 2);
-    //for decoder
-    memset (&info, 0, sizeof (SFrameBSInfo));
-    //set a fixed random value
-    iRandValue = rand() % 256;
+bool EncodeDecodeTestBase::prepareEncDecParam (const EncodeDecodeFileParamBase EncDecFileParam) {
+  //for encoder
+  //I420: 1(Y) + 1/4(U) + 1/4(V)
+  int frameSize = EncDecFileParam.width * EncDecFileParam.height * 3 / 2;
+  buf_.SetLength (frameSize);
+  if (buf_.Length() != (size_t)frameSize) {
+    printf ("buf_.Length() failed! frameSize = %d\n", frameSize);
+    return false;
+  }
+  memset (&EncPic, 0, sizeof (SSourcePicture));
+  EncPic.iPicWidth = EncDecFileParam.width;
+  EncPic.iPicHeight = EncDecFileParam.height;
+  EncPic.iColorFormat = videoFormatI420;
+  EncPic.iStride[0] = EncPic.iPicWidth;
+  EncPic.iStride[1] = EncPic.iStride[2] = EncPic.iPicWidth >> 1;
+  EncPic.pData[0] = buf_.data();
+  EncPic.pData[1] = EncPic.pData[0] + EncDecFileParam.width * EncDecFileParam.height;
+  EncPic.pData[2] = EncPic.pData[1] + (EncDecFileParam.width * EncDecFileParam.height >> 2);
+  //for decoder
+  memset (&info, 0, sizeof (SFrameBSInfo));
+  //set a fixed random value
+  iRandValue = rand() % 256;
+  return true;
 }
 
 void EncodeDecodeTestBase::encToDecData (const SFrameBSInfo& info, int& len) {
-    len = 0;
-    for (int i = 0; i < info.iLayerNum; ++i) {
-      const SLayerBSInfo& layerInfo = info.sLayerInfo[i];
-      for (int j = 0; j < layerInfo.iNalCount; ++j) {
-        len += layerInfo.pNalLengthInByte[j];
-      }
+  len = 0;
+  for (int i = 0; i < info.iLayerNum; ++i) {
+    const SLayerBSInfo& layerInfo = info.sLayerInfo[i];
+    for (int j = 0; j < layerInfo.iNalCount; ++j) {
+      len += layerInfo.pNalLengthInByte[j];
     }
+  }
 }
 
-void EncodeDecodeTestBase::encToDecSliceData (const int iLayerNum, const int iSliceNum, const SFrameBSInfo& info, int& len) {
-    ASSERT_TRUE (iLayerNum < MAX_LAYER_NUM_OF_FRAME);
-    len = 0;
-    const SLayerBSInfo& layerInfo = info.sLayerInfo[iLayerNum];
-    if (iSliceNum < layerInfo.iNalCount)
-      len = layerInfo.pNalLengthInByte[iSliceNum];
+void EncodeDecodeTestBase::encToDecSliceData (const int iLayerNum, const int iSliceNum, const SFrameBSInfo& info,
+    int& len) {
+  ASSERT_TRUE (iLayerNum < MAX_LAYER_NUM_OF_FRAME);
+  len = 0;
+  const SLayerBSInfo& layerInfo = info.sLayerInfo[iLayerNum];
+  if (iSliceNum < layerInfo.iNalCount)
+    len = layerInfo.pNalLengthInByte[iSliceNum];
 }
 
-void EncodeDecodeTestAPIBase::prepareParam0 (int iLayers, int iSlices, int width, int height, float framerate, SEncParamExt* pParam) {
-    memset (pParam, 0, sizeof (SEncParamExt));
-    EncodeDecodeTestBase::prepareParam (iLayers, iSlices, width, height, framerate, pParam);
+void EncodeDecodeTestAPIBase::prepareParam0 (int iLayers, int iSlices, int width, int height, float framerate,
+    SEncParamExt* pParam) {
+  memset (pParam, 0, sizeof (SEncParamExt));
+  EncodeDecodeTestBase::prepareParam (iLayers, iSlices, width, height, framerate, pParam);
 }
 
-void EncodeDecodeTestAPIBase::prepareParamDefault (int iLayers, int iSlices, int width, int height, float framerate, SEncParamExt* pParam) {
-    memset (pParam, 0, sizeof (SEncParamExt));
-    encoder_->GetDefaultParams (pParam);
-    EncodeDecodeTestBase::prepareParam (iLayers, iSlices, width, height, framerate, pParam);
+void EncodeDecodeTestAPIBase::prepareParamDefault (int iLayers, int iSlices, int width, int height, float framerate,
+    SEncParamExt* pParam) {
+  memset (pParam, 0, sizeof (SEncParamExt));
+  encoder_->GetDefaultParams (pParam);
+  EncodeDecodeTestBase::prepareParam (iLayers, iSlices, width, height, framerate, pParam);
 }
 
 
 void EncodeDecodeTestAPIBase::EncodeOneFrame (int iCheckTypeIndex) {
-    int frameSize = EncPic.iPicWidth * EncPic.iPicHeight * 3 / 2;
-    int lumaSize = EncPic.iPicWidth * EncPic.iPicHeight;
-    memset (buf_.data(), iRandValue, lumaSize);
-    memset (buf_.data() + lumaSize, rand() % 256, (frameSize - lumaSize));
-    int rv = encoder_->EncodeFrame (&EncPic, &info);
-    if (0 == iCheckTypeIndex)
-      ASSERT_TRUE (rv == cmResultSuccess);
-    else if (1 == iCheckTypeIndex)
-      ASSERT_TRUE (rv == cmResultSuccess || rv == cmUnknownReason);
+  int frameSize = EncPic.iPicWidth * EncPic.iPicHeight * 3 / 2;
+  int lumaSize = EncPic.iPicWidth * EncPic.iPicHeight;
+  memset (buf_.data(), iRandValue, lumaSize);
+  memset (buf_.data() + lumaSize, rand() % 256, (frameSize - lumaSize));
+  int rv = encoder_->EncodeFrame (&EncPic, &info);
+  if (0 == iCheckTypeIndex)
+    ASSERT_TRUE (rv == cmResultSuccess);
+  else if (1 == iCheckTypeIndex)
+    ASSERT_TRUE (rv == cmResultSuccess || rv == cmUnknownReason);
 }
 
-void EncodeDecodeTestAPIBase::EncDecOneFrame (const int iWidth, const int iHeight, const int iFrame, FILE* pfEnc) {
-    int iLen = 0, rv;
-    InitialEncDec (iWidth, iHeight);
-    EncodeOneFrame (iFrame);
+bool EncodeDecodeTestAPIBase::EncDecOneFrame (const int iWidth, const int iHeight, const int iFrame, FILE* pfEnc) {
+  int iLen = 0, rv;
+  if (!InitialEncDec (iWidth, iHeight))
+    return false;
+  EncodeOneFrame (iFrame);
 
-    //extract target layer data
-    encToDecData (info, iLen);
-    //call decoder
-    unsigned char* pData[3] = { NULL };
-    memset (&dstBufInfo_, 0, sizeof (SBufferInfo));
-    rv = decoder_->DecodeFrameNoDelay (info.sLayerInfo[0].pBsBuf, iLen, pData, &dstBufInfo_);
-    EXPECT_TRUE (rv == cmResultSuccess) << " rv = " << rv << " iFrameIdx = " << iFrame;
-    if (NULL != pfEnc) {
-      fwrite (info.sLayerInfo[0].pBsBuf, iLen, 1, pfEnc);
-    }
+  //extract target layer data
+  encToDecData (info, iLen);
+  //call decoder
+  unsigned char* pData[3] = { NULL };
+  memset (&dstBufInfo_, 0, sizeof (SBufferInfo));
+  rv = decoder_->DecodeFrameNoDelay (info.sLayerInfo[0].pBsBuf, iLen, pData, &dstBufInfo_);
+  EXPECT_TRUE (rv == cmResultSuccess) << " rv = " << rv << " iFrameIdx = " << iFrame;
+  if (NULL != pfEnc) {
+    fwrite (info.sLayerInfo[0].pBsBuf, iLen, 1, pfEnc);
+  }
+  return true;
 }
 
-void EncodeDecodeTestAPIBase::TestOneSimulcastAVC (SEncParamExt* pParam, ISVCDecoder** decoder, unsigned char** pBsBuf, int iSpatialLayerNum,
-                            int iEncFrameNum,
-                            int iSaveFileIdx) {
-    int aLen[MAX_SPATIAL_LAYER_NUM] = {0, 0, 0, 0};
+bool EncodeDecodeTestAPIBase::TestOneSimulcastAVC (SEncParamExt* pParam, ISVCDecoder** decoder, unsigned char** pBsBuf,
+    int iSpatialLayerNum,
+    int iEncFrameNum,
+    int iSaveFileIdx) {
+  int aLen[MAX_SPATIAL_LAYER_NUM] = {0, 0, 0, 0};
 
-    FILE* fEnc[MAX_SPATIAL_LAYER_NUM];
-    if (iSaveFileIdx == 1) {
-      fEnc[0] = fopen ("enc00.264", "wb");
-      fEnc[1] = fopen ("enc01.264", "wb");
-      fEnc[2] = fopen ("enc02.264", "wb");
-      fEnc[3] = fopen ("enc03.264", "wb");
-    } else if (iSaveFileIdx > 1) {
-      fEnc[0] = fopen ("enc10.264", "wb");
-      fEnc[1] = fopen ("enc11.264", "wb");
-      fEnc[2] = fopen ("enc12.264", "wb");
-      fEnc[3] = fopen ("enc13.264", "wb");
-    }
+  FILE* fEnc[MAX_SPATIAL_LAYER_NUM];
+  if (iSaveFileIdx == 1) {
+    fEnc[0] = fopen ("enc00.264", "wb");
+    fEnc[1] = fopen ("enc01.264", "wb");
+    fEnc[2] = fopen ("enc02.264", "wb");
+    fEnc[3] = fopen ("enc03.264", "wb");
+  } else if (iSaveFileIdx > 1) {
+    fEnc[0] = fopen ("enc10.264", "wb");
+    fEnc[1] = fopen ("enc11.264", "wb");
+    fEnc[2] = fopen ("enc12.264", "wb");
+    fEnc[3] = fopen ("enc13.264", "wb");
+  }
 
-    int rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, pParam);
-    ASSERT_TRUE (rv == cmResultSuccess) << "SetOption Failed pParam: rv = " << rv;
+  int rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, pParam);
+  if (rv != cmResultSuccess) {
+    printf ("SetOption Failed, rv = %d\n", rv);
+    return false;
+  }
 
-    int iIdx;
-    //begin testing
-    for (int iFrame = 0; iFrame < iEncFrameNum; iFrame++) {
-      int iResult;
-      int iLayerLen = 0;
-      unsigned char* pData[3] = { NULL };
+  int iIdx;
+  //begin testing
+  for (int iFrame = 0; iFrame < iEncFrameNum; iFrame++) {
+    int iResult;
+    int iLayerLen = 0;
+    unsigned char* pData[3] = { NULL };
 
-      InitialEncDec (pParam->iPicWidth, pParam->iPicHeight);
-      EncodeOneFrame (0);
+    if (!InitialEncDec (pParam->iPicWidth, pParam->iPicHeight))
+      return false;
+    EncodeOneFrame (0);
 
-      // init
-      for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
-        aLen[iIdx] = 0;
+    // init
+    for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
+      aLen[iIdx] = 0;
+    }
+    for (int iLayer = 0; iLayer < info.iLayerNum; ++iLayer) {
+      iLayerLen = 0;
+      const SLayerBSInfo& layerInfo = info.sLayerInfo[iLayer];
+      for (int iNal = 0; iNal < layerInfo.iNalCount; ++iNal) {
+        iLayerLen += layerInfo.pNalLengthInByte[iNal];
       }
-      for (int iLayer = 0; iLayer < info.iLayerNum; ++iLayer) {
-        iLayerLen = 0;
-        const SLayerBSInfo& layerInfo = info.sLayerInfo[iLayer];
-        for (int iNal = 0; iNal < layerInfo.iNalCount; ++iNal) {
-          iLayerLen += layerInfo.pNalLengthInByte[iNal];
-        }
 
-        iIdx = layerInfo.uiSpatialId;
-        EXPECT_TRUE (iIdx < iSpatialLayerNum) << "iIdx = " << iIdx << ", iSpatialLayerNum = " << iSpatialLayerNum;
-        memcpy ((pBsBuf[iIdx] + aLen[iIdx]), layerInfo.pBsBuf, iLayerLen * sizeof (unsigned char));
-        aLen[iIdx] += iLayerLen;
-      }
+      iIdx = layerInfo.uiSpatialId;
+      EXPECT_TRUE (iIdx < iSpatialLayerNum) << "iIdx = " << iIdx << ", iSpatialLayerNum = " << iSpatialLayerNum;
+      memcpy ((pBsBuf[iIdx] + aLen[iIdx]), layerInfo.pBsBuf, iLayerLen * sizeof (unsigned char));
+      aLen[iIdx] += iLayerLen;
+    }
 
-      for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
-        pData[0] = pData[1] = pData[2] = 0;
-        memset (&dstBufInfo_, 0, sizeof (SBufferInfo));
+    for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
+      pData[0] = pData[1] = pData[2] = 0;
+      memset (&dstBufInfo_, 0, sizeof (SBufferInfo));
 
-        if (iSaveFileIdx > 0) {
-          fwrite (pBsBuf[iIdx], aLen[iIdx], 1, fEnc[iIdx]);
-        }
+      if (iSaveFileIdx > 0) {
+        fwrite (pBsBuf[iIdx], aLen[iIdx], 1, fEnc[iIdx]);
+      }
 
-        iResult = decoder[iIdx]->DecodeFrame2 (pBsBuf[iIdx], aLen[iIdx], pData, &dstBufInfo_);
-        EXPECT_TRUE (iResult == cmResultSuccess) << "iResult=" << iResult << ", LayerIdx=" << iIdx;
+      iResult = decoder[iIdx]->DecodeFrame2 (pBsBuf[iIdx], aLen[iIdx], pData, &dstBufInfo_);
+      EXPECT_TRUE (iResult == cmResultSuccess) << "iResult=" << iResult << ", LayerIdx=" << iIdx;
 
-        iResult = decoder[iIdx]->DecodeFrame2 (NULL, 0, pData, &dstBufInfo_);
-        EXPECT_TRUE (iResult == cmResultSuccess) << "iResult=" << iResult << ", LayerIdx=" << iIdx;
-        EXPECT_EQ (dstBufInfo_.iBufferStatus, 1) << "LayerIdx=" << iIdx;
-      }
+      iResult = decoder[iIdx]->DecodeFrame2 (NULL, 0, pData, &dstBufInfo_);
+      EXPECT_TRUE (iResult == cmResultSuccess) << "iResult=" << iResult << ", LayerIdx=" << iIdx;
+      EXPECT_EQ (dstBufInfo_.iBufferStatus, 1) << "LayerIdx=" << iIdx;
     }
+  }
 
   if (iSaveFileIdx > 0) {
     fclose (fEnc[0]);
@@ -180,6 +195,7 @@
     fclose (fEnc[2]);
     fclose (fEnc[3]);
   }
+  return true;
 }
 
 long IsKeyFrameLost (ISVCDecoder* pDecoder, SLTRRecoverRequest* p_LTR_Recover_Request, long hr) {
--- a/test/api/encode_decode_api_test.h
+++ b/test/api/encode_decode_api_test.h
@@ -87,7 +87,7 @@
 
   virtual void prepareParam (int iLayers, int iSlices, int width, int height, float framerate, SEncParamExt* pParam);
 
-  virtual void prepareEncDecParam (const EncodeDecodeFileParamBase EncDecFileParam);
+  virtual bool prepareEncDecParam (const EncodeDecodeFileParamBase EncDecFileParam);
 
   virtual void encToDecData (const SFrameBSInfo& info, int& len);
 
@@ -129,7 +129,7 @@
 
   void prepareParamDefault (int iLayers, int iSlices, int width, int height, float framerate, SEncParamExt* pParam);
 
-  void InitialEncDec (int iWidth, int iHeight);
+  bool InitialEncDec (int iWidth, int iHeight);
   void RandomParamExtCombination();
   void ValidateParamExtCombination();
   void SliceParamValidationForMode2 (int iSpatialIdx);
@@ -137,8 +137,8 @@
   void SliceParamValidationForMode4();
 
   void EncodeOneFrame (int iCheckTypeIndex);
-  void EncDecOneFrame (const int iWidth, const int iHeight, const int iFrame, FILE* pfEnc);
-  void TestOneSimulcastAVC (SEncParamExt* pParam, ISVCDecoder** decoder, unsigned char** pBsBuf, int iSpatialLayerNum,
+  bool EncDecOneFrame (const int iWidth, const int iHeight, const int iFrame, FILE* pfEnc);
+  bool TestOneSimulcastAVC (SEncParamExt* pParam, ISVCDecoder** decoder, unsigned char** pBsBuf, int iSpatialLayerNum,
                             int iEncFrameNum,
                             int iCallTimes);
 };
--- a/test/api/encode_decode_api_test.template
+++ b/test/api/encode_decode_api_test.template
@@ -72,7 +72,7 @@
 
     //FillParam
 
-    InitialEncDec (param_.iPicWidth, param_.iPicHeight);
+    ASSERT_TRUE (InitialEncDec (param_.iPicWidth, param_.iPicHeight));
     rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &param_);
     ASSERT_TRUE (rv == cmResultSuccess) << "SetOption Failed pParam: rv = " << rv;
     EncodeOneFrame (0);
--- a/test/api/encode_options_test.cpp
+++ b/test/api/encode_options_test.cpp
@@ -10,13 +10,16 @@
 #include "encode_decode_api_test.h"
 using namespace WelsCommon;
 
-void EncodeDecodeTestAPIBase::InitialEncDec (int iWidth, int iHeight) {
+bool EncodeDecodeTestAPIBase::InitialEncDec (int iWidth, int iHeight) {
   // for encoder
   // I420: 1(Y) + 1/4(U) + 1/4(V)
   int frameSize = iWidth * iHeight * 3 / 2;
 
   buf_.SetLength (frameSize);
-  ASSERT_TRUE (buf_.Length() == (size_t)frameSize);
+  if (buf_.Length() != (size_t)frameSize) {
+    printf ("buf_.Length() failed! frameSize = %d\n", frameSize);
+    return false;
+  }
 
   memset (&EncPic, 0, sizeof (SSourcePicture));
   EncPic.iPicWidth    = iWidth;
@@ -33,7 +36,9 @@
 
   //set a fixed random value
   iRandValue = rand() % 256;
+  return true;
 }
+
 void EncodeDecodeTestAPIBase::RandomParamExtCombination() {
 
   param_.iPicWidth  = WelsClip3 ((((rand() % MAX_WIDTH) >> 1)  + 1) << 1, 2, MAX_WIDTH);
@@ -321,7 +326,7 @@
     // so need to enhance the validation check for any random value of each variable in ParamExt
 
     if (cmResultSuccess == iResult) {
-      InitialEncDec (param_.iPicWidth, param_.iPicHeight);
+      ASSERT_TRUE (InitialEncDec (param_.iPicWidth, param_.iPicHeight));
       EncodeOneFrame (0);
       encToDecData (info, len);
       pData[0] = pData[1] = pData[2] = 0;
@@ -366,7 +371,7 @@
     iResult = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_BASE, &param_);
 
     if (cmResultSuccess == iResult) {
-      InitialEncDec (param_.iPicWidth, param_.iPicHeight);
+      ASSERT_TRUE (InitialEncDec (param_.iPicWidth, param_.iPicHeight));
       EncodeOneFrame (0);
       encToDecData (info, len);
       pData[0] = pData[1] = pData[2] = 0;
@@ -439,34 +444,34 @@
   int rv = encoder_->InitializeExt (&sParam1);
   ASSERT_TRUE (rv == cmResultSuccess) << "InitializeExt: rv = " << rv << " at " << sParam1.iPicWidth << "x" <<
                                       sParam1.iPicHeight;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
   // new IDR
   rv = encoder_->ForceIntraFrame (true);
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
   // step#2: pParam2
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam2);
   ASSERT_TRUE (rv == cmResultSuccess) << "SetOption: rv = " << rv << " at " << sParam2.iPicWidth << "x" <<
                                       sParam2.iPicHeight;
-  EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc));
 
   // new IDR
   rv = encoder_->ForceIntraFrame (true);
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv;
-  EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc));
 
   // step#3: back to pParam1
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam1);
   ASSERT_TRUE (rv == cmResultSuccess) << "SetOption: rv = " << rv << " at " << sParam1.iPicWidth << "x" <<
                                       sParam1.iPicHeight;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
   // step#4: back to pParam2
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam2);
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv << sParam2.iPicWidth << sParam2.iPicHeight;
-  EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc));
 
 #ifdef DEBUG_FILE_SAVE_INCREASING_ID
   fclose (fEnc);
@@ -487,15 +492,15 @@
   fEnc = fopen ("enc_INCREASING_ID2.264", "wb");
 #endif
   iEncFrameNum = 0;
-  EncDecOneFrame (sParam3.iPicWidth, sParam3.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam3.iPicWidth, sParam3.iPicHeight, iEncFrameNum++, fEnc));
 
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam2);
   ASSERT_TRUE (rv == cmResultSuccess) << "SetOption Failed sParam2: rv = " << rv;
-  EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc));
 
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam1);
   ASSERT_TRUE (rv == cmResultSuccess) << "SetOption Failed sParam2: rv = " << rv;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
 #ifdef DEBUG_FILE_SAVE_INCREASING_ID
   fclose (fEnc);
@@ -547,36 +552,37 @@
   //int TraceLevel = WELS_LOG_INFO;
   //encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &TraceLevel);
   int rv = encoder_->InitializeExt (&sParam1);
+  printf ("Start EncDecOneFrame()\n");
   ASSERT_TRUE (rv == cmResultSuccess) << "InitializeExt: rv = " << rv << " at " << sParam1.iPicWidth << "x" <<
                                       sParam1.iPicHeight;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
   // new IDR
   rv = encoder_->ForceIntraFrame (true);
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
   // step#2: pParam2
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam2);
   ASSERT_TRUE (rv == cmResultSuccess) << "SetOption: rv = " << rv << " at " << sParam2.iPicWidth << "x" <<
                                       sParam2.iPicHeight;
-  EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc));
 
   // new IDR
   rv = encoder_->ForceIntraFrame (true);
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv;
-  EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc));
 
   // step#3: back to pParam1
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam1);
   ASSERT_TRUE (rv == cmResultSuccess) << "SetOption: rv = " << rv << " at " << sParam1.iPicWidth << "x" <<
                                       sParam1.iPicHeight;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
   // step#4: back to pParam2
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam2);
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv << sParam2.iPicWidth << sParam2.iPicHeight;
-  EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc));
 
 #ifdef DEBUG_FILE_SAVE2
   fclose (fEnc);
@@ -597,15 +603,15 @@
   fEnc = fopen ("enc_SPS_LISTING_AND_PPS_INCREASING11.264", "wb");
 #endif
   iEncFrameNum = 0;
-  EncDecOneFrame (sParam3.iPicWidth, sParam3.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam3.iPicWidth, sParam3.iPicHeight, iEncFrameNum++, fEnc));
 
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam2);
   ASSERT_TRUE (rv == cmResultSuccess) << "SetOption Failed sParam2: rv = " << rv;
-  EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc));
 
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam1);
   ASSERT_TRUE (rv == cmResultSuccess) << "SetOption Failed sParam2: rv = " << rv;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
 #ifdef DEBUG_FILE_SAVE2
   fclose (fEnc);
@@ -657,12 +663,12 @@
   // step#3: set back to pParam1, with a smaller num_ref, it still uses the previous SPS
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam1);
   ASSERT_TRUE (rv == cmResultSuccess) << "SetOption Failed sParam1: rv = " << rv;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
   // new IDR, PPS increases
   rv = encoder_->ForceIntraFrame (true);
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
   rv = encoder_->Uninitialize();
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv;
@@ -719,7 +725,7 @@
                                         sParam2.iPicWidth;
   } // end of setting loop
 
-  EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc));
 
 #ifdef DEBUG_FILE_SAVE2
   fclose (fEnc);
@@ -765,13 +771,13 @@
   int rv = encoder_->InitializeExt (&sParam1);
   ASSERT_TRUE (rv == cmResultSuccess) << "InitializeExt: rv = " << rv << " at " << sParam1.iPicWidth << "x" <<
                                       sParam1.iPicHeight;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
   // new IDR
   rv = encoder_->ForceIntraFrame (true);
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
   rv = encoder_->Uninitialize();
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv;
@@ -841,18 +847,18 @@
   // step#2: pParam2
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam2);
   ASSERT_TRUE (rv == cmResultSuccess) << "SetOption Failed sParam2: rv = " << rv;
-  EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc));
 
   // step#3: back to pParam1, SHOULD NOT encounter ERROR
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam1);
   ASSERT_TRUE (rv == cmResultSuccess) << "SetOption: rv = " << rv << " at " << sParam1.iPicWidth << "x" <<
                                       sParam1.iPicHeight;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
   // new IDR
   rv = encoder_->ForceIntraFrame (true);
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv;
-  EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam1.iPicWidth, sParam1.iPicHeight, iEncFrameNum++, fEnc));
 
   rv = encoder_->Uninitialize();
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv;
@@ -906,7 +912,7 @@
                                       sParam1.iPicHeight;
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam2);
   ASSERT_TRUE (rv == cmResultSuccess) << "SetOption Failed sParam2: rv = " << rv;
-  EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc));
 
   // step#2: set strategy for success
   int32_t iNewStra = SPS_PPS_LISTING;
@@ -921,7 +927,7 @@
   rv = encoder_->SetOption (ENCODER_OPTION_SPS_PPS_ID_STRATEGY, &iNewStra);
   ASSERT_TRUE (rv != cmResultSuccess);
 
-  EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc);
+  ASSERT_TRUE (EncDecOneFrame (sParam2.iPicWidth, sParam2.iPicHeight, iEncFrameNum++, fEnc));
 
   // step#4: pParam3, SHOULD encounter ERROR
   rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sParam3);
@@ -966,12 +972,12 @@
   int iIdx = 0;
   for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
     pBsBuf[iIdx] = static_cast<unsigned char*> (malloc (iWidth * iHeight * 3 * sizeof (unsigned char) / 2));
-    EXPECT_TRUE (pBsBuf[iIdx] != NULL);
+    ASSERT_TRUE (pBsBuf[iIdx] != NULL);
     aLen[iIdx] = 0;
 
     long rv = WelsCreateDecoder (&decoder[iIdx]);
     ASSERT_EQ (0, rv);
-    EXPECT_TRUE (decoder[iIdx] != NULL);
+    ASSERT_TRUE (decoder[iIdx] != NULL);
 
     SDecodingParam decParam;
     memset (&decParam, 0, sizeof (SDecodingParam));
@@ -988,7 +994,7 @@
     int iLayerLen = 0;
     unsigned char* pData[3] = { NULL };
 
-    InitialEncDec (param_.iPicWidth, param_.iPicHeight);
+    ASSERT_TRUE (InitialEncDec (param_.iPicWidth, param_.iPicHeight));
     EncodeOneFrame (0);
 
     iLayerLen = 0;
@@ -1083,12 +1089,12 @@
   //create decoder
   for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
     pBsBuf[iIdx] = static_cast<unsigned char*> (malloc (iWidth * iHeight * 3 * sizeof (unsigned char) / 2));
-    EXPECT_TRUE (pBsBuf[iIdx] != NULL);
+    ASSERT_TRUE (pBsBuf[iIdx] != NULL);
     aLen[iIdx] = 0;
 
     long rv = WelsCreateDecoder (&decoder[iIdx]);
     ASSERT_EQ (0, rv);
-    EXPECT_TRUE (decoder[iIdx] != NULL);
+    ASSERT_TRUE (decoder[iIdx] != NULL);
 
     SDecodingParam decParam;
     memset (&decParam, 0, sizeof (SDecodingParam));
@@ -1106,7 +1112,7 @@
     int iLayerLen = 0;
     unsigned char* pData[3] = { NULL };
 
-    InitialEncDec (param_.iPicWidth, param_.iPicHeight);
+    ASSERT_TRUE (InitialEncDec (param_.iPicWidth, param_.iPicHeight));
     EncodeOneFrame (0);
 
     // init
@@ -1197,11 +1203,11 @@
   //create decoder
   for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
     pBsBuf[iIdx] = static_cast<unsigned char*> (malloc (iWidth * iHeight * 3 * sizeof (unsigned char) / 2));
-    EXPECT_TRUE (pBsBuf[iIdx] != NULL);
+    ASSERT_TRUE (pBsBuf[iIdx] != NULL);
 
     long rv = WelsCreateDecoder (&decoder[iIdx]);
     ASSERT_EQ (0, rv);
-    EXPECT_TRUE (decoder[iIdx] != NULL);
+    ASSERT_TRUE (decoder[iIdx] != NULL);
 
     SDecodingParam decParam;
     memset (&decParam, 0, sizeof (SDecodingParam));
@@ -1213,8 +1219,8 @@
     ASSERT_EQ (0, rv);
   }
 
-  TestOneSimulcastAVC (&sParam1, decoder, pBsBuf, iSpatialLayerNum, iEncFrameNum, 0);
-  TestOneSimulcastAVC (&sParam2, decoder, pBsBuf, iSpatialLayerNum, iEncFrameNum, 0);
+  ASSERT_TRUE (TestOneSimulcastAVC (&sParam1, decoder, pBsBuf, iSpatialLayerNum, iEncFrameNum, 0));
+  ASSERT_TRUE (TestOneSimulcastAVC (&sParam2, decoder, pBsBuf, iSpatialLayerNum, iEncFrameNum, 0));
 
   for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
     free (pBsBuf[iIdx]);
@@ -1367,9 +1373,8 @@
     encoder_->Uninitialize();
     int rv = encoder_->InitializeExt (&param_);
     ASSERT_TRUE (rv == cmResultSuccess);
-    InitialEncDec (p.iWidth, p.iHeight);
+    ASSERT_TRUE (InitialEncDec (p.iWidth, p.iHeight));
 
-
     int32_t iSpsPpsIdAddition = 1;
     encoder_->SetOption (ENCODER_OPTION_SPS_PPS_ID_STRATEGY, &iSpsPpsIdAddition);
     int32_t iIDRPeriod = (int32_t) pow (2.0f, (param_.iTemporalLayerNum - 1)) * ((rand() % 5) + 1);
@@ -1456,12 +1461,12 @@
   //create decoder
   for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
     pBsBuf[iIdx] = static_cast<unsigned char*> (malloc (iWidth * iHeight * 3 * sizeof (unsigned char) / 2));
-    EXPECT_TRUE (pBsBuf[iIdx] != NULL);
+    ASSERT_TRUE (pBsBuf[iIdx] != NULL);
     aLen[iIdx] = 0;
 
     long rv = WelsCreateDecoder (&decoder[iIdx]);
     ASSERT_EQ (0, rv);
-    EXPECT_TRUE (decoder[iIdx] != NULL);
+    ASSERT_TRUE (decoder[iIdx] != NULL);
 
     SDecodingParam decParam;
     memset (&decParam, 0, sizeof (SDecodingParam));
@@ -1494,7 +1499,7 @@
       int iLayerLen = 0;
       unsigned char* pData[3] = { NULL };
 
-      InitialEncDec (param_.iPicWidth, param_.iPicHeight);
+      ASSERT_TRUE (InitialEncDec (param_.iPicWidth, param_.iPicHeight));
       EncodeOneFrame (0);
 
       if (iInsertIdr == iFrame) {
@@ -1597,11 +1602,11 @@
   //create decoder
   for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
     pBsBuf[iIdx] = static_cast<unsigned char*> (malloc (iWidth * iHeight * 3 * sizeof (unsigned char) / 2));
-    EXPECT_TRUE (pBsBuf[iIdx] != NULL);
+    ASSERT_TRUE (pBsBuf[iIdx] != NULL);
 
     long rv = WelsCreateDecoder (&decoder[iIdx]);
     ASSERT_EQ (0, rv);
-    EXPECT_TRUE (decoder[iIdx] != NULL);
+    ASSERT_TRUE (decoder[iIdx] != NULL);
 
     SDecodingParam decParam;
     memset (&decParam, 0, sizeof (SDecodingParam));
@@ -1613,10 +1618,13 @@
     ASSERT_EQ (0, rv);
   }
 
-  TestOneSimulcastAVC (&sParam, decoder, pBsBuf, iSpatialLayerNum, iEncFrameNum, 0);
+  ASSERT_TRUE (TestOneSimulcastAVC (&sParam, decoder, pBsBuf, iSpatialLayerNum, iEncFrameNum, 0));
 
   for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
-    free (pBsBuf[iIdx]);
+    if (pBsBuf[iIdx]) {
+      free (pBsBuf[iIdx]);
+      pBsBuf[iIdx] = NULL;
+    }
 
     if (decoder[iIdx] != NULL) {
       decoder[iIdx]->Uninitialize();
@@ -1665,11 +1673,11 @@
   //create decoder
   for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
     pBsBuf[iIdx] = static_cast<unsigned char*> (malloc (iWidth * iHeight * 3 * sizeof (unsigned char) / 2));
-    EXPECT_TRUE (pBsBuf[iIdx] != NULL);
+    ASSERT_TRUE (pBsBuf[iIdx] != NULL);
 
     long rv = WelsCreateDecoder (&decoder[iIdx]);
     ASSERT_EQ (0, rv);
-    EXPECT_TRUE (decoder[iIdx] != NULL);
+    ASSERT_TRUE (decoder[iIdx] != NULL);
 
     SDecodingParam decParam;
     memset (&decParam, 0, sizeof (SDecodingParam));
@@ -1681,10 +1689,11 @@
     ASSERT_EQ (0, rv);
   }
 
-  TestOneSimulcastAVC (&sParam, decoder, pBsBuf, iSpatialLayerNum, iEncFrameNum, 0);
+  ASSERT_TRUE (TestOneSimulcastAVC (&sParam, decoder, pBsBuf, iSpatialLayerNum, iEncFrameNum, 0));
 
   for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
-    free (pBsBuf[iIdx]);
+    if (pBsBuf[iIdx])
+      free (pBsBuf[iIdx]);
 
     if (decoder[iIdx] != NULL) {
       decoder[iIdx]->Uninitialize();
@@ -1725,11 +1734,11 @@
   //create decoder
   for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
     pBsBuf[iIdx] = static_cast<unsigned char*> (malloc (iWidth * iHeight * 3 * sizeof (unsigned char) / 2));
-    EXPECT_TRUE (pBsBuf[iIdx] != NULL);
+    ASSERT_TRUE (pBsBuf[iIdx] != NULL);
 
     long rv = WelsCreateDecoder (&decoder[iIdx]);
     ASSERT_EQ (0, rv);
-    EXPECT_TRUE (decoder[iIdx] != NULL);
+    ASSERT_TRUE (decoder[iIdx] != NULL);
 
     SDecodingParam decParam;
     memset (&decParam, 0, sizeof (SDecodingParam));
@@ -1741,7 +1750,7 @@
     ASSERT_EQ (0, rv);
   }
 
-  TestOneSimulcastAVC (&sParam, decoder, pBsBuf, iSpatialLayerNum, iEncFrameNum, 0);
+  ASSERT_TRUE (TestOneSimulcastAVC (&sParam, decoder, pBsBuf, iSpatialLayerNum, iEncFrameNum, 0));
 
   for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
     free (pBsBuf[iIdx]);
@@ -1790,11 +1799,11 @@
   //create decoder
   for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
     pBsBuf[iIdx] = static_cast<unsigned char*> (malloc (iWidth * iHeight * 3 * sizeof (unsigned char) / 2));
-    EXPECT_TRUE (pBsBuf[iIdx] != NULL);
+    ASSERT_TRUE (pBsBuf[iIdx] != NULL);
 
     long rv = WelsCreateDecoder (&decoder[iIdx]);
     ASSERT_EQ (0, rv);
-    EXPECT_TRUE (decoder[iIdx] != NULL);
+    ASSERT_TRUE (decoder[iIdx] != NULL);
 
     SDecodingParam decParam;
     memset (&decParam, 0, sizeof (SDecodingParam));
@@ -1815,7 +1824,7 @@
     int iLayerLen = 0;
     unsigned char* pData[3] = { NULL };
 
-    InitialEncDec (sParam.iPicWidth, sParam.iPicHeight);
+    ASSERT_TRUE (InitialEncDec (sParam.iPicWidth, sParam.iPicHeight));
     int frameSize = EncPic.iPicWidth * EncPic.iPicHeight * 3 / 2;
     memset (buf_.data(), rand() % 256, (frameSize >> 2));
     memset (buf_.data() + (frameSize >> 2), rand() % 256, (frameSize - (frameSize >> 2)));
@@ -1920,16 +1929,16 @@
   int iSliceNum        = 1;
 
   EProfileIdc profileList[11] = {PRO_UNKNOWN, PRO_BASELINE, PRO_MAIN,
-                               PRO_EXTENDED , PRO_HIGH , PRO_HIGH10,
-                               PRO_HIGH422, PRO_HIGH444, PRO_CAVLC444,
-                               PRO_SCALABLE_BASELINE,
-                               PRO_SCALABLE_HIGH
-                              };
+                                 PRO_EXTENDED , PRO_HIGH , PRO_HIGH10,
+                                 PRO_HIGH422, PRO_HIGH444, PRO_CAVLC444,
+                                 PRO_SCALABLE_BASELINE,
+                                 PRO_SCALABLE_HIGH
+                                };
   ELevelIdc levelList[18] = {  LEVEL_UNKNOWN, LEVEL_1_0, LEVEL_1_B, LEVEL_1_1, LEVEL_1_2,
-                             LEVEL_1_3, LEVEL_2_0, LEVEL_2_1, LEVEL_2_2, LEVEL_3_0,
-                             LEVEL_3_1, LEVEL_3_2, LEVEL_4_0, LEVEL_4_1,
-                             LEVEL_4_2, LEVEL_5_0, LEVEL_5_1, LEVEL_5_2
-                          };
+                               LEVEL_1_3, LEVEL_2_0, LEVEL_2_1, LEVEL_2_2, LEVEL_3_0,
+                               LEVEL_3_1, LEVEL_3_2, LEVEL_4_0, LEVEL_4_1,
+                               LEVEL_4_2, LEVEL_5_0, LEVEL_5_1, LEVEL_5_2
+                            };
 
   EProfileIdc iEncProfileIdc = PRO_UNKNOWN;
   ELevelIdc iEncLevelIdc =  LEVEL_UNKNOWN;
@@ -1958,7 +1967,7 @@
   int rv = encoder_->InitializeExt (&sParam);
   ASSERT_TRUE (rv == cmResultSuccess) << "InitializeExt: rv = " << rv << " at " << sParam.iPicWidth << "x" <<
                                       sParam.iPicHeight;
-  EncDecOneFrame (sParam.iPicWidth, sParam.iPicHeight, iEncFrameNum++, NULL);
+  ASSERT_TRUE (EncDecOneFrame (sParam.iPicWidth, sParam.iPicHeight, iEncFrameNum++, NULL));
 
   decoder_->GetOption (DECODER_OPTION_PROFILE, &iDecProfileIdc);
 
@@ -2021,7 +2030,7 @@
   rv = encoder_->ForceIntraFrame (true);
   ASSERT_TRUE (rv == cmResultSuccess) << "rv = " << rv;
 
-  EncDecOneFrame (sParam.iPicWidth, sParam.iPicHeight, iEncFrameNum++, NULL);
+  ASSERT_TRUE (EncDecOneFrame (sParam.iPicWidth, sParam.iPicHeight, iEncFrameNum++, NULL));
 
   decoder_->GetOption (DECODER_OPTION_PROFILE, &iDecProfileIdc);
 
--- a/test/api/ltr_test.cpp
+++ b/test/api/ltr_test.cpp
@@ -18,7 +18,7 @@
   int rv = encoder_->InitializeExt (&param_);
   ASSERT_TRUE (rv == cmResultSuccess);
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
 
   int32_t iTraceLevel = WELS_LOG_QUIET;
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
@@ -116,7 +116,7 @@
   ASSERT_TRUE (rv == cmResultSuccess);
   m_LTR_Recover_Request.uiFeedbackType = NO_RECOVERY_REQUSET;
 
-  InitialEncDec (p.width, p.height);
+  ASSERT_TRUE (InitialEncDec (p.width, p.height));
 
   int32_t iTraceLevel = WELS_LOG_QUIET;
   encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &iTraceLevel);
--- a/test/common/ExpandPicture.cpp
+++ b/test/common/ExpandPicture.cpp
@@ -127,9 +127,11 @@
       int32_t iBuffHeight = iPicHeight + H264_PADDING_LENGTH_LUMA * 2;
       int32_t iBuffSize =  iBuffHeight * iStride * sizeof (uint8_t);
       uint8_t* pAnchorDstBuff = static_cast<uint8_t*> (WelsMallocz (iBuffSize, "pAnchorDstBuff"));
+      ASSERT_TRUE (pAnchorDstBuff != NULL) << "pAnchorDstBuff alloc failed for size:" << iBuffSize;
       uint8_t* pAnchorDst = pAnchorDstBuff + H264_PADDING_LENGTH_LUMA * iStride + H264_PADDING_LENGTH_LUMA;
 
       uint8_t* pTestDstBuff = static_cast<uint8_t*> (WelsMallocz (iBuffSize, "pTestDstBuff"));
+      ASSERT_TRUE (pTestDstBuff != NULL) << "pTestDstBuff alloc failed for size:" << iBuffSize;
       uint8_t* pTestDst = pTestDstBuff + H264_PADDING_LENGTH_LUMA * iStride + H264_PADDING_LENGTH_LUMA;
 
       // Generate Src
@@ -173,9 +175,11 @@
       int32_t iBuffHeight = iPicHeight + H264_PADDING_LENGTH_CHROMA * 2;
       int32_t iBuffSize =  iBuffHeight * iStride * sizeof (uint8_t);
       uint8_t* pAnchorDstBuff = static_cast<uint8_t*> (WelsMallocz (iBuffSize, "pAnchorDstBuff"));
+      ASSERT_TRUE (pAnchorDstBuff != NULL) << "pAnchorDstBuff alloc failed for size:" << iBuffSize;
       uint8_t* pAnchorDst = pAnchorDstBuff + H264_PADDING_LENGTH_CHROMA * iStride + H264_PADDING_LENGTH_CHROMA;
 
       uint8_t* pTestDstBuff = static_cast<uint8_t*> (WelsMallocz (iBuffSize, "pTestDstBuff"));
+      ASSERT_TRUE (pTestDstBuff != NULL) << "pTestDstBuff alloc failed for size:" << iBuffSize;
       uint8_t* pTestDst = pTestDstBuff + H264_PADDING_LENGTH_CHROMA * iStride + H264_PADDING_LENGTH_CHROMA;
 
       // Generate Src
@@ -218,8 +222,10 @@
       if (uiCpuFlag & WELS_CPU_SSE2) {
         iPicWidth = WELS_ALIGN (iPicWidth, 32);
       }
-      iStride[0]                  = WELS_ALIGN (iPicWidth, MB_WIDTH_LUMA)   + (PADDING_LENGTH << 1);      // with width of horizon
-      int32_t iPicHeightExt       = WELS_ALIGN (iPicHeight, MB_HEIGHT_LUMA) + (PADDING_LENGTH << 1);      // with height of vertical
+      iStride[0]                  = WELS_ALIGN (iPicWidth,
+                                    MB_WIDTH_LUMA)   + (PADDING_LENGTH << 1);      // with width of horizon
+      int32_t iPicHeightExt       = WELS_ALIGN (iPicHeight,
+                                    MB_HEIGHT_LUMA) + (PADDING_LENGTH << 1);      // with height of vertical
       iStride[1]                  = iStride[0] >> 1;
       int32_t iPicChromaHeightExt = iPicHeightExt >> 1;
       iStride[2]                  = iStride[1];
@@ -227,11 +233,13 @@
       int32_t iChromaSize         = iStride[1] * iPicChromaHeightExt;
 
       pPicAnchorBuffer = static_cast<uint8_t*> (WelsMallocz (iLumaSize + (iChromaSize << 1), "pPicAnchor"));
+      ASSERT_TRUE (pPicAnchorBuffer != NULL) << "pPicAnchorBuffer alloc failed for size:" << (iLumaSize + (iChromaSize << 1));
       pPicAnchor[0]     = pPicAnchorBuffer + (1 + iStride[0]) * PADDING_LENGTH;
       pPicAnchor[1]     = pPicAnchorBuffer + iLumaSize + (((1 + iStride[1]) * PADDING_LENGTH) >> 1);
       pPicAnchor[2]     = pPicAnchorBuffer + iLumaSize + iChromaSize + (((1 + iStride[2]) * PADDING_LENGTH) >> 1);
 
       pPicTestBuffer = static_cast<uint8_t*> (WelsMallocz (iLumaSize + (iChromaSize << 1), "pPicTest"));
+      ASSERT_TRUE (pPicTestBuffer != NULL) << "pPicTestBuffer alloc failed for size:" << (iLumaSize + (iChromaSize << 1));
       pPicTest[0]       = pPicTestBuffer + (1 + iStride[0]) * PADDING_LENGTH;
       pPicTest[1]       = pPicTestBuffer + iLumaSize + (((1 + iStride[1]) * PADDING_LENGTH) >> 1);
       pPicTest[2]       = pPicTestBuffer + iLumaSize + iChromaSize + (((1 + iStride[2]) * PADDING_LENGTH) >> 1);
--- a/test/common/WelsThreadPoolTest.cpp
+++ b/test/common/WelsThreadPoolTest.cpp
@@ -39,7 +39,9 @@
 void* OneCallingFunc() {
   CThreadPoolTest cThreadPoolTest;
   CSimpleTask* aTasks[TEST_TASK_NUM];
-  CWelsThreadPool* pThreadPool = & (CWelsThreadPool::AddReference());
+  CWelsThreadPool* pThreadPool = (CWelsThreadPool::AddReference());
+  if (pThreadPool == NULL)
+    return 0;
 
   int32_t  i;
   for (i = 0; i < TEST_TASK_NUM; i++) {
@@ -70,9 +72,11 @@
   EXPECT_EQ (0, iRet);
   EXPECT_FALSE (CWelsThreadPool::IsReferenced());
 
-  CWelsThreadPool* pThreadPool = & (CWelsThreadPool::AddReference ());
-  EXPECT_TRUE(pThreadPool->IsReferenced());
+  CWelsThreadPool* pThreadPool = (CWelsThreadPool::AddReference());
+  ASSERT_TRUE (pThreadPool != NULL);
 
+  EXPECT_TRUE (pThreadPool->IsReferenced());
+
   EXPECT_EQ (8, pThreadPool->GetThreadNum());
 
   iRet = CWelsThreadPool::SetThreadNum (4);
@@ -84,7 +88,7 @@
   iRet = CWelsThreadPool::SetThreadNum (4);
   EXPECT_EQ (0, iRet);
 
-  pThreadPool = & (CWelsThreadPool::AddReference ());
+  pThreadPool = (CWelsThreadPool::AddReference());
   EXPECT_TRUE (pThreadPool->IsReferenced());
   EXPECT_EQ (4, pThreadPool->GetThreadNum());
   pThreadPool->RemoveInstance();
@@ -97,24 +101,24 @@
   int iCallingNum = 10;
   WELS_THREAD_HANDLE mThreadID[30];
   int i = 0;
-
+  WELS_THREAD_ERROR_CODE rc;
   for (i = 0; i < iCallingNum; i++) {
-    WelsThreadCreate (& (mThreadID[i]), (LPWELS_THREAD_ROUTINE)OneCallingFunc, NULL, 0);
+    rc = WelsThreadCreate (& (mThreadID[i]), (LPWELS_THREAD_ROUTINE)OneCallingFunc, NULL, 0);
+    ASSERT_TRUE (rc == WELS_THREAD_ERROR_OK);
     WelsSleep (1);
   }
-
   for (i = iCallingNum; i < iCallingNum * 2; i++) {
-    WelsThreadCreate (& (mThreadID[i]), (LPWELS_THREAD_ROUTINE)OneCallingFunc, NULL, 0);
+    rc = WelsThreadCreate (& (mThreadID[i]), (LPWELS_THREAD_ROUTINE)OneCallingFunc, NULL, 0);
+    ASSERT_TRUE (rc == WELS_THREAD_ERROR_OK);
     WelsSleep (1);
     WelsThreadJoin (mThreadID[i]);
   }
-
   for (i = 0; i < iCallingNum; i++) {
     WelsThreadJoin (mThreadID[i]);
   }
-
   for (i = iCallingNum * 2; i < iCallingNum * 3; i++) {
-    WelsThreadCreate (& (mThreadID[i]), (LPWELS_THREAD_ROUTINE)OneCallingFunc, NULL, 0);
+    rc = WelsThreadCreate (& (mThreadID[i]), (LPWELS_THREAD_ROUTINE)OneCallingFunc, NULL, 0);
+    ASSERT_TRUE (rc == WELS_THREAD_ERROR_OK);
     WelsSleep (1);
     WelsThreadJoin (mThreadID[i]);
   }
--- a/test/utils/BufferedData.h
+++ b/test/utils/BufferedData.h
@@ -1,6 +1,7 @@
 #ifndef __BUFFEREDDATA_H__
 #define __BUFFEREDDATA_H__
 
+#include <gtest/gtest.h>
 #include <stddef.h>
 #include <stdlib.h>
 #include "../test_stdint.h"
@@ -46,6 +47,8 @@
   void SetLength (size_t newLen) {
     if (EnsureCapacity (newLen)) {
       length_ = newLen;
+    } else {
+      FAIL () << "unable to alloc memory in SetLength()";
     }
   }