diff --git a/libavcodec/pthread.c b/libavcodec/pthread.c
index 00febde426f92bf20cc4a5186a170382b551476a..244bb520dd97008aa16005e095bc1f479d13786f 100644
--- a/libavcodec/pthread.c
+++ b/libavcodec/pthread.c
@@ -97,7 +97,7 @@ void avcodec_thread_free(AVCodecContext *avctx)
     pthread_cond_destroy(&c->current_job_cond);
     pthread_cond_destroy(&c->last_job_cond);
     av_free(c->workers);
-    av_free(c);
+    av_freep(&avctx->thread_opaque);
 }
 
 int avcodec_thread_execute(AVCodecContext *avctx, action_t* func, void **arg, int *ret, int job_count)