Free user supplied data when deleting a frame
authorKieran Kunhya <kieran@kunhya.com>
Tue, 24 Jul 2012 15:47:45 +0000 (08:47 -0700)
committerJason Garrett-Glaser <jason@x264.com>
Thu, 26 Jul 2012 14:42:05 +0000 (07:42 -0700)
This eliminates a memory leak when calling x264_encoder_close.

common/frame.c
encoder/encoder.c

index b0694f6..2e8629e 100644 (file)
@@ -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 );
     }
index cdff8ec..6c894e8 100644 (file)
@@ -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 )