diff --git a/libavformat/mov.c b/libavformat/mov.c
index f4c35d7d2e56c7a80ff336684bd504ace800c81d..134953efd93ee5ce3e761a9030a28fb089584de3 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -4830,8 +4830,9 @@ static int mov_read_close(AVFormatContext *s)
         av_freep(&sc->rap_group);
         av_freep(&sc->display_matrix);
 
-        for (j = 0; j < sc->stsd_count; j++)
-            av_free(sc->extradata[j]);
+        if (sc->extradata)
+            for (j = 0; j < sc->stsd_count; j++)
+                av_free(sc->extradata[j]);
         av_freep(&sc->extradata);
         av_freep(&sc->extradata_size);