From 370a468fbe90003b60eafd3b81b1224358d7b060 Mon Sep 17 00:00:00 2001 From: David FORT Date: Fri, 29 Jan 2016 15:31:18 +0100 Subject: [PATCH] Stop created threads if the creation of thread fails If we fail to create a thread, we must stop the one that have been already created or we have these threads that work on freed memory. --- codec/encoder/core/src/slice_multi_threading.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/codec/encoder/core/src/slice_multi_threading.cpp b/codec/encoder/core/src/slice_multi_threading.cpp index ad006d9e..2a82cbdd 100644 --- a/codec/encoder/core/src/slice_multi_threading.cpp +++ b/codec/encoder/core/src/slice_multi_threading.cpp @@ -961,7 +961,7 @@ int32_t CreateSliceThreads (sWelsEncCtx* pCtx) { while (iIdx < kiThreadCount) { if (WelsThreadCreate (&pCtx->pSliceThreading->pThreadHandles[iIdx], CodingSliceThreadProc, &pCtx->pSliceThreading->pThreadPEncCtx[iIdx], 0)) { - return 1; + goto out_fail; } @@ -969,6 +969,17 @@ int32_t CreateSliceThreads (sWelsEncCtx* pCtx) { } MT_TRACE_LOG (pCtx, WELS_LOG_INFO, "CreateSliceThreads() exit.."); return 0; + +out_fail: + iIdx--; + while (iIdx >= 0) { + WelsEventSignal(&pCtx->pSliceThreading->pExitEncodeEvent[iIdx]); + WelsEventSignal(&pCtx->pSliceThreading->pThreadMasterEvent[iIdx]); + WelsThreadJoin(pCtx->pSliceThreading->pThreadHandles[iIdx]); + pCtx->pSliceThreading->pThreadHandles[iIdx] = 0; + iIdx--; + } + return 1; } int32_t FiredSliceThreads (sWelsEncCtx* pCtx, SSliceThreadPrivateData* pPriData, WELS_EVENT* pEventsList,