diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c
index bdef856aa6c7375f91da1108c38b06ff96e2e71c..257d5986f5471b430e66ffba7ea2a33c4c88cb32 100644
--- a/libavcodec/movtextdec.c
+++ b/libavcodec/movtextdec.c
@@ -121,6 +121,9 @@ static void mov_text_cleanup(MovTextContext *m)
 static void mov_text_cleanup_ftab(MovTextContext *m)
 {
     int i;
+    if (m->ftab_temp)
+        av_freep(&m->ftab_temp->font);
+    av_freep(&m->ftab_temp);
     if (m->ftab) {
         for(i = 0; i < m->count_f; i++) {
             av_freep(&m->ftab[i]->font);
@@ -210,7 +213,7 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m)
             m->ftab_entries = 0;
             return -1;
         }
-        m->ftab_temp = av_malloc(sizeof(*m->ftab_temp));
+        m->ftab_temp = av_mallocz(sizeof(*m->ftab_temp));
         if (!m->ftab_temp) {
             mov_text_cleanup_ftab(m);
             return AVERROR(ENOMEM);
@@ -237,6 +240,7 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m)
             mov_text_cleanup_ftab(m);
             return AVERROR(ENOMEM);
         }
+        m->ftab_temp = NULL;
         tx3g_ptr = tx3g_ptr + font_length;
     }
     for (i = 0; i < m->ftab_entries; i++) {