shithub: openh264

Download patch

ref: 13586a3dfcfad347f78a9d0d2d07757b2ab5aac3
parent: f791ac28ec12794a3605541c2760ea814e08c621
parent: e07ee9c09694f179b872701e505a05d407709bd2
author: ruil2 <ruil2@cisco.com>
date: Thu Feb 18 05:25:19 EST 2016

Merge pull request #2366 from sijchen/fix_free6

[Encoder] add error handling in memory allocation failed case for multi-threading

--- a/codec/common/src/WelsThreadPool.cpp
+++ b/codec/common/src/WelsThreadPool.cpp
@@ -38,6 +38,7 @@
  *************************************************************************************
  */
 #include "typedefs.h"
+#include "memory_align.h"
 #include "WelsThreadPool.h"
 
 namespace WelsCommon {
@@ -50,7 +51,21 @@
   m_cBusyThreads = new CWelsList<CWelsTaskThread>();
   m_iMaxThreadNum = 0;
 
-  Init (iMaxThreadNum);
+  if (NULL == m_cWaitedTasks || NULL == m_cIdleThreads || NULL == m_cBusyThreads) {
+    WELS_DELETE_OP(m_cWaitedTasks);
+    WELS_DELETE_OP(m_cIdleThreads);
+    WELS_DELETE_OP(m_cBusyThreads);
+
+    return;
+  }
+
+  if (WELS_THREAD_ERROR_OK != Init (iMaxThreadNum)) {
+    Uninit();
+
+    WELS_DELETE_OP(m_cWaitedTasks);
+    WELS_DELETE_OP(m_cIdleThreads);
+    WELS_DELETE_OP(m_cBusyThreads);
+  }
 }
 
 
@@ -57,9 +72,9 @@
 CWelsThreadPool::~CWelsThreadPool() {
   Uninit();
 
-  delete m_cWaitedTasks;
-  delete m_cIdleThreads;
-  delete m_cBusyThreads;
+  WELS_DELETE_OP(m_cWaitedTasks);
+  WELS_DELETE_OP(m_cIdleThreads);
+  WELS_DELETE_OP(m_cBusyThreads);
 }
 
 WELS_THREAD_ERROR_CODE CWelsThreadPool::OnTaskStart (CWelsTaskThread* pThread, IWelsTask* pTask) {
@@ -175,7 +190,9 @@
     return WELS_THREAD_ERROR_GENERAL;
   }
 
-  pThread->Start();
+  if (WELS_THREAD_ERROR_OK != pThread->Start()) {
+    return WELS_THREAD_ERROR_GENERAL;
+  }
   AddThreadToIdleQueue (pThread);
 
   return WELS_THREAD_ERROR_OK;
@@ -183,7 +200,7 @@
 
 void  CWelsThreadPool::DestroyThread (CWelsTaskThread* pThread) {
   pThread->Kill();
-  delete pThread;
+  WELS_DELETE_OP(pThread);
 
   return;
 }
--- a/codec/encoder/core/src/encoder_ext.cpp
+++ b/codec/encoder/core/src/encoder_ext.cpp
@@ -2290,8 +2290,7 @@
     if ((*ppCtx)->pMemAlign != NULL) {
       WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_INFO, "FreeMemorySvc(), verify memory usage (%d bytes) after free..",
                (*ppCtx)->pMemAlign->WelsGetMemoryUsage());
-      delete (*ppCtx)->pMemAlign;
-      (*ppCtx)->pMemAlign = NULL;
+      WELS_DELETE_OP((*ppCtx)->pMemAlign);
     }
 
     free (*ppCtx);
@@ -2479,8 +2478,7 @@
 
   if ((*ppCtx)->pVpp) {
     (*ppCtx)->pVpp->FreeSpatialPictures (*ppCtx);
-    delete (*ppCtx)->pVpp;
-    (*ppCtx)->pVpp = NULL;
+    WELS_DELETE_OP((*ppCtx)->pVpp);
   }
   FreeMemorySvc (ppCtx);
   *ppCtx = NULL;
--- a/codec/encoder/core/src/slice_multi_threading.cpp
+++ b/codec/encoder/core/src/slice_multi_threading.cpp
@@ -413,8 +413,7 @@
   memset (&pSmt->bThreadBsBufferUsage, 0, MAX_THREADS_NUM * sizeof (bool));
 
   if ((*ppCtx)->pTaskManage != NULL) {
-    delete (*ppCtx)->pTaskManage;
-    (*ppCtx)->pTaskManage = NULL;
+    WELS_DELETE_OP((*ppCtx)->pTaskManage);
   }
 
 #ifdef MT_DEBUG
--- a/codec/encoder/core/src/wels_task_management.cpp
+++ b/codec/encoder/core/src/wels_task_management.cpp
@@ -63,9 +63,11 @@
 
   IWelsTaskManage* pTaskManage;
   pTaskManage = WELS_NEW_OP (CWelsTaskManageBase(), CWelsTaskManageBase);
+  WELS_VERIFY_RETURN_IF (NULL, NULL == pTaskManage)
 
-  if (pTaskManage) {
-    pTaskManage->Init (pCtx);
+  if ( ENC_RETURN_SUCCESS != pTaskManage->Init (pCtx) ) {
+    pTaskManage->Uninit();
+    WELS_DELETE_OP(pTaskManage);
   }
   return pTaskManage;
 }
@@ -98,7 +100,7 @@
                                WelsCommon::CWelsThreadPool);
   WELS_VERIFY_RETURN_IF (ENC_RETURN_MEMALLOCERR, NULL == m_pThreadPool)
 
-  int32_t iReturn = 0;
+  int32_t iReturn = ENC_RETURN_SUCCESS;
   for (int32_t iDid = 0; iDid < MAX_DEPENDENCY_LAYER; iDid++) {
     m_pcAllTaskList[CWelsBaseTask::WELS_ENC_TASK_ENCODING][iDid] = m_cEncodingTaskList[iDid];
     m_pcAllTaskList[CWelsBaseTask::WELS_ENC_TASK_UPDATEMBMAP][iDid] = m_cPreEncodingTaskList[iDid];
@@ -114,8 +116,8 @@
   WELS_DELETE_OP (m_pThreadPool);
 
   for (int32_t iDid = 0; iDid < MAX_DEPENDENCY_LAYER; iDid++) {
-    delete m_cEncodingTaskList[iDid];
-    delete m_cPreEncodingTaskList[iDid];
+    WELS_DELETE_OP(m_cEncodingTaskList[iDid]);
+    WELS_DELETE_OP(m_cPreEncodingTaskList[iDid]);
   }
   WelsEventClose (&m_hTaskEvent);
 }