diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index a0b810e0d7e8be54f30be14be99ba8d6929c2ddd..37c496cd15ca32fc9d1b80a8513e28035bc0eb5a 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -2017,6 +2017,7 @@ av_cold int ff_ac3_encode_close(AVCodecContext *avctx)
     AC3EncodeContext *s = avctx->priv_data;
 
     av_freep(&s->windowed_samples);
+    if (s->planar_samples)
     for (ch = 0; ch < s->channels; ch++)
         av_freep(&s->planar_samples[ch]);
     av_freep(&s->planar_samples);