[x264-devel] Free user supplied data when deleting a frame

Kieran Kunhya git at videolan.org
Wed Sep 5 21:07:20 CEST 2012


x264 | branch: master | Kieran Kunhya <kieran at kunhya.com> | Tue Jul 24 08:47:45 2012 -0700| [68dfb7b352c4d273e44668c1f6e4a9a283a37e84] | committer: Jason Garrett-Glaser

Free user supplied data when deleting a frame
This eliminates a memory leak when calling x264_encoder_close.

> http://git.videolan.org/gitweb.cgi/x264.git/?a=commit;h=68dfb7b352c4d273e44668c1f6e4a9a283a37e84
---

 common/frame.c    |   10 ++++++++++
 encoder/encoder.c |   18 ++++++++++++++++--
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/common/frame.c b/common/frame.c
index b0694f6..2e8629e 100644
--- a/common/frame.c
+++ b/common/frame.c
@@ -300,6 +300,16 @@ void x264_frame_delete( x264_frame_t *frame )
             x264_free( frame->mv16x16-1 );
         x264_free( frame->ref[0] );
         x264_free( frame->ref[1] );
+        if( frame->param && frame->param->param_free )
+            frame->param->param_free( frame->param );
+        if( frame->mb_info_free )
+            frame->mb_info_free( frame->mb_info );
+        if( frame->extra_sei.sei_free )
+        {
+            for( int i = 0; i < frame->extra_sei.num_payloads; i++ )
+                frame->extra_sei.sei_free( frame->extra_sei.payloads[i].payload );
+            frame->extra_sei.sei_free( frame->extra_sei.payloads );
+        }
         x264_pthread_mutex_destroy( &frame->mutex );
         x264_pthread_cond_destroy( &frame->cv );
     }
diff --git a/encoder/encoder.c b/encoder/encoder.c
index cdff8ec..6c894e8 100644
--- a/encoder/encoder.c
+++ b/encoder/encoder.c
@@ -2878,7 +2878,10 @@ int     x264_encoder_encode( x264_t *h,
     {
         x264_encoder_reconfig( h, h->fenc->param );
         if( h->fenc->param->param_free )
+        {
             h->fenc->param->param_free( h->fenc->param );
+            h->fenc->param = NULL;
+        }
     }
 
     // ok to call this before encoding any frames, since the initial values of fdec have b_kept_as_ref=0
@@ -3098,12 +3101,19 @@ int     x264_encoder_encode( x264_t *h,
         if( x264_nal_end( h ) )
             return -1;
         overhead += h->out.nal[h->out.i_nal-1].i_payload + NALU_OVERHEAD - (h->param.b_annexb && h->out.i_nal-1);
-        if( h->fenc->extra_sei.sei_free && h->fenc->extra_sei.payloads[i].payload )
+        if( h->fenc->extra_sei.sei_free )
+        {
             h->fenc->extra_sei.sei_free( h->fenc->extra_sei.payloads[i].payload );
+            h->fenc->extra_sei.payloads[i].payload = NULL;
+        }
     }
 
-    if( h->fenc->extra_sei.sei_free && h->fenc->extra_sei.payloads )
+    if( h->fenc->extra_sei.sei_free )
+    {
         h->fenc->extra_sei.sei_free( h->fenc->extra_sei.payloads );
+        h->fenc->extra_sei.payloads = NULL;
+        h->fenc->extra_sei.sei_free = NULL;
+    }
 
     if( h->fenc->b_keyframe )
     {
@@ -3232,7 +3242,11 @@ static int x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
     x264_emms();
 
     if( h->fenc->mb_info_free )
+    {
         h->fenc->mb_info_free( h->fenc->mb_info );
+        h->fenc->mb_info = NULL;
+        h->fenc->mb_info_free = NULL;
+    }
 
     /* generate buffering period sei and insert it into place */
     if( h->i_thread_frames > 1 && h->fenc->b_keyframe && h->sps->vui.b_nal_hrd_parameters_present )



More information about the x264-devel mailing list