[x264-devel] Fwd: Huge memory leak in x264_encoder_open

BugMaster BugMaster at narod.ru
Wed Oct 10 09:56:50 CEST 2007


I make a patch for fixing memory leak (in encoder.c) specialy when more then 1 thread used. Diff:
diff -uNr trunk-r680/encoder/encoder.c trunk/encoder/encoder.c
--- trunk-r680/encoder/encoder.c	2007-09-24 11:24:28.000000000 +0400
+++ trunk/encoder/encoder.c	2007-10-10 10:44:48.206576000 +0400
@@ -1769,7 +1769,11 @@
     {
         // don't strictly have to wait for the other threads, but it's simpler than cancelling them
         if( h->thread[i]->b_thread_active )
+        {
             x264_pthread_join( h->thread[i]->thread_handle, NULL );
+            assert( h->thread[i]->fenc->i_reference_count == 1 );
+            x264_frame_delete( h->thread[i]->fenc );
+        }
     }
 
 #ifdef DEBUG_BENCHMARK
@@ -1934,16 +1938,6 @@
             x264_log( h, X264_LOG_INFO, "kb/s:%.1f\n", f_bitrate );
     }
 
-    /* frames */
-    for( i = 0; h->frames.current[i]; i++ )
-        x264_frame_delete( h->frames.current[i] );
-    for( i = 0; h->frames.next[i]; i++ )
-        x264_frame_delete( h->frames.next[i] );
-    for( i = 0; h->frames.unused[i]; i++ )
-        x264_frame_delete( h->frames.unused[i] );
-    for( i = 0; h->frames.reference[i]; i++ )
-        x264_frame_delete( h->frames.reference[i] );
-
     /* rc */
     x264_ratecontrol_delete( h );
 
@@ -1956,8 +1950,46 @@
         free( h->param.rc.psz_rc_eq );
 
     x264_cqm_delete( h );
+
+    if( h->param.i_threads > 1)
+        h = h->thread[ h->i_thread_phase % h->param.i_threads ];
+
+    /* frames */
+    for( i = 0; h->frames.current[i]; i++ )
+    {
+        assert( h->frames.current[i]->i_reference_count == 1 );
+        x264_frame_delete( h->frames.current[i] );
+    }
+    for( i = 0; h->frames.next[i]; i++ )
+    {
+        assert( h->frames.next[i]->i_reference_count == 1 );
+        x264_frame_delete( h->frames.next[i] );
+    }
+    for( i = 0; h->frames.unused[i]; i++ )
+    {
+        assert( h->frames.unused[i]->i_reference_count == 0 );
+        x264_frame_delete( h->frames.unused[i] );
+    }
+
+    h = h->thread[0];
+
     for( i = h->param.i_threads - 1; i >= 0; i-- )
     {
+        x264_frame_t **frame;
+
+        for( frame = h->thread[i]->frames.reference; *frame; frame++ )
+        {
+            assert( (*frame)->i_reference_count > 0 );
+            (*frame)->i_reference_count--;
+            if( (*frame)->i_reference_count == 0 )
+                x264_frame_delete( *frame );
+        }
+        frame = &h->thread[i]->fdec;
+        assert( (*frame)->i_reference_count > 0 );
+        (*frame)->i_reference_count--;
+        if( (*frame)->i_reference_count == 0 )
+            x264_frame_delete( *frame );
+
         x264_macroblock_cache_end( h->thread[i] );
         x264_free( h->thread[i]->out.p_bitstream );
         x264_free( h->thread[i] );




More information about the x264-devel mailing list