diff --git a/libavformat/rtpenc_mpegts.c b/libavformat/rtpenc_mpegts.c
index 8ad446b9dda9b5fcc2a681532c054ced3c219e4c..8ced6a9d4995aa28c71be153f7ded138ca989980 100644
--- a/libavformat/rtpenc_mpegts.c
+++ b/libavformat/rtpenc_mpegts.c
@@ -98,7 +98,7 @@ static int rtp_mpegts_write_header(AVFormatContext *s)
 
 fail:
     if (mpegts_ctx) {
-        ffio_free_dyn_buf(&chain->mpegts_ctx->pb);
+        ffio_free_dyn_buf(&mpegts_ctx->pb);
         avformat_free_context(mpegts_ctx);
     }
     if (rtp_ctx)