From 8c8affab7f5e74e8d5fdfe2e0a28b604bb626ede Mon Sep 17 00:00:00 2001 From: jtorresfabra Date: Wed, 27 Feb 2019 13:01:45 +0100 Subject: [PATCH] ThreadPool was allocating memory for threads and pools locally to the image object. As that image could not be freed from cache, for each sink operation done for that image the code was creating new pools and threads without freeing the previous one. SO the more number of threads and the more operations the bigger the hidden leak was. The fix just creates the objects not locally to that image, and also free them in its destroy functions --- libvips/iofuncs/threadpool.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/libvips/iofuncs/threadpool.c b/libvips/iofuncs/threadpool.c index 3d5b84c1..3793018f 100644 --- a/libvips/iofuncs/threadpool.c +++ b/libvips/iofuncs/threadpool.c @@ -579,6 +579,9 @@ vips_thread_free( VipsThread *thr ) VIPS_FREEF( g_object_unref, thr->state ); thr->pool = NULL; + /* Free the thread memory + */ + g_free(thr); } static int @@ -703,7 +706,7 @@ vips_thread_new( VipsThreadpool *pool ) { VipsThread *thr; - if( !(thr = VIPS_NEW( pool->im, VipsThread )) ) + if( !(thr = VIPS_NEW( NULL, VipsThread )) ) return( NULL ); thr->pool = pool; thr->state = NULL; @@ -738,7 +741,9 @@ vips_threadpool_kill_threads( VipsThreadpool *pool ) vips_thread_free( pool->thr[i] ); pool->thr[i] = NULL; } - + /* Free the thread array + */ + g_free(pool->thr); pool->thr = NULL; VIPS_DEBUG_MSG( "vips_threadpool_kill_threads: " @@ -758,7 +763,9 @@ vips_threadpool_free( VipsThreadpool *pool ) VIPS_FREEF( vips_g_mutex_free, pool->allocate_lock ); vips_semaphore_destroy( &pool->finish ); vips_semaphore_destroy( &pool->tick ); - + /* Free pool memory + */ + g_free(pool); return( 0 ); } @@ -779,7 +786,7 @@ vips_threadpool_new( VipsImage *im ) /* Allocate and init new thread block. */ - if( !(pool = VIPS_NEW( im, VipsThreadpool )) ) + if( !(pool = VIPS_NEW( NULL, VipsThreadpool )) ) return( NULL ); pool->im = im; pool->allocate = NULL; @@ -802,11 +809,6 @@ vips_threadpool_new( VipsImage *im ) n_tiles = VIPS_CLIP( 0, n_tiles, MAX_THREADS ); pool->nthr = VIPS_MIN( pool->nthr, n_tiles ); - /* Attach tidy-up callback. - */ - g_signal_connect( im, "close", - G_CALLBACK( vips_threadpool_new_cb ), pool ); - VIPS_DEBUG_MSG( "vips_threadpool_new: \"%s\" (%p), with %d threads\n", im->filename, pool, pool->nthr ); @@ -824,7 +826,7 @@ vips_threadpool_create_threads( VipsThreadpool *pool ) /* Make thread array. */ - if( !(pool->thr = VIPS_ARRAY( pool->im, pool->nthr, VipsThread * )) ) + if( !(pool->thr = VIPS_ARRAY( NULL, pool->nthr, VipsThread * )) ) return( -1 ); for( i = 0; i < pool->nthr; i++ ) pool->thr[i] = NULL;