Fix 5212886: free the reader after freeing decoders.

If we free the reader first, the decoder may still read from it
after the reader is freed. So we need to free the reader last.

Change-Id: Ib6c7d6991d544870f1b3892387d0db312d501755
diff --git a/libvideoeditor/vss/mcs/src/M4MCS_API.c b/libvideoeditor/vss/mcs/src/M4MCS_API.c
index 5eb2a75..baa0dd5 100755
--- a/libvideoeditor/vss/mcs/src/M4MCS_API.c
+++ b/libvideoeditor/vss/mcs/src/M4MCS_API.c
@@ -10092,7 +10092,48 @@
 
     M4OSA_TRACE2_1("M4MCS_intCleanUp_ReadersDecoders called with pC=0x%x", pC);
 
+    /**/
+    /* ----- Free video decoder stuff, if needed ----- */
+
+    if( M4OSA_NULL != pC->pViDecCtxt )
+    {
+        err = pC->m_pVideoDecoder->m_pFctDestroy(pC->pViDecCtxt);
+        pC->pViDecCtxt = M4OSA_NULL;
+
+        if( M4NO_ERROR != err )
+        {
+            M4OSA_TRACE1_1(
+                "M4MCS_cleanUp: m_pVideoDecoder->pFctDestroy returns 0x%x",
+                err);
+            /**< don't return, we still have stuff to free */
+        }
+    }
+
+    /* ----- Free the audio decoder stuff ----- */
+
+    if( M4OSA_NULL != pC->pAudioDecCtxt )
+    {
+        err = pC->m_pAudioDecoder->m_pFctDestroyAudioDec(pC->pAudioDecCtxt);
+        pC->pAudioDecCtxt = M4OSA_NULL;
+
+        if( M4NO_ERROR != err )
+        {
+            M4OSA_TRACE1_1(
+                "M4MCS_cleanUp: m_pAudioDecoder->m_pFctDestroyAudioDec returns 0x%x",
+                err);
+            /**< don't return, we still have stuff to free */
+        }
+    }
+
+    if( M4OSA_NULL != pC->AudioDecBufferOut.m_dataAddress )
+    {
+        free(pC->AudioDecBufferOut.m_dataAddress);
+        pC->AudioDecBufferOut.m_dataAddress = M4OSA_NULL;
+    }
+
     /* ----- Free reader stuff, if needed ----- */
+    // We cannot free the reader before decoders because the decoders may read
+    // from the reader (in another thread) before being stopped.
 
     if( M4OSA_NULL != pC->
         pReaderContext ) /**< may be M4OSA_NULL if M4MCS_open was not called */
@@ -10140,44 +10181,6 @@
         free(pC->m_pDataVideoAddress2);
         pC->m_pDataVideoAddress2 = M4OSA_NULL;
     }
-    /**/
-    /* ----- Free video decoder stuff, if needed ----- */
-
-    if( M4OSA_NULL != pC->pViDecCtxt )
-    {
-        err = pC->m_pVideoDecoder->m_pFctDestroy(pC->pViDecCtxt);
-        pC->pViDecCtxt = M4OSA_NULL;
-
-        if( M4NO_ERROR != err )
-        {
-            M4OSA_TRACE1_1(
-                "M4MCS_cleanUp: m_pVideoDecoder->pFctDestroy returns 0x%x",
-                err);
-            /**< don't return, we still have stuff to free */
-        }
-    }
-
-    /* ----- Free the audio decoder stuff ----- */
-
-    if( M4OSA_NULL != pC->pAudioDecCtxt )
-    {
-        err = pC->m_pAudioDecoder->m_pFctDestroyAudioDec(pC->pAudioDecCtxt);
-        pC->pAudioDecCtxt = M4OSA_NULL;
-
-        if( M4NO_ERROR != err )
-        {
-            M4OSA_TRACE1_1(
-                "M4MCS_cleanUp: m_pAudioDecoder->m_pFctDestroyAudioDec returns 0x%x",
-                err);
-            /**< don't return, we still have stuff to free */
-        }
-    }
-
-    if( M4OSA_NULL != pC->AudioDecBufferOut.m_dataAddress )
-    {
-        free(pC->AudioDecBufferOut.m_dataAddress);
-        pC->AudioDecBufferOut.m_dataAddress = M4OSA_NULL;
-    }
 
     return M4NO_ERROR;
 }