From 1909b31bd6555c0ef313ac46bb30af3bd34a109a Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Fri, 10 Jun 2016 13:58:01 +0100 Subject: [PATCH] vips_image_write() only refs input when it has to when you write to a non-partial image, you create a sink ... so vips_image_write() only needs to ref the input when writing to partials this change makes it much easier to (for example) combine many images in bounded space, see for example: https://gist.github.com/jcupitt/c516325ebef7601b5da610af5619c9b2 --- ChangeLog | 2 ++ libvips/iofuncs/image.c | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index cc98c971..e82cc29f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,6 +17,8 @@ - free pixel buffers on image close as well as thread exit ... stops main thread buffers clogging up the system - dzsave can write compressed zips [Felix Bünemann] +- vips_image_write() only refs the input when it has to ... makes it easier to + combine many images in bounded memory 18/5/16 started 8.3.2 - more robust vips image reading diff --git a/libvips/iofuncs/image.c b/libvips/iofuncs/image.c index ced8d367..2168f83a 100644 --- a/libvips/iofuncs/image.c +++ b/libvips/iofuncs/image.c @@ -8,6 +8,8 @@ * - add vips_image_copy_memory() * 25/11/15 * - add vips_image_new_from_memory_copy() + * 10/6/16 + * - vips_image_write() does not ref input for non-partial images */ /* @@ -2448,17 +2450,22 @@ vips_image_write( VipsImage *image, VipsImage *out ) VIPS_DEMAND_STYLE_THINSTRIP, image, NULL ) ) return( -1 ); - /* We generate from @image partially, so we need to keep it about as - * long as @out is about. - */ - g_object_ref( image ); - vips_object_local( out, image ); - if( vips_image_generate( out, vips_start_one, vips_image_write_gen, vips_stop_one, image, NULL ) ) return( -1 ); + /* If @out is a partial image, we need to make sure that @image stays + * alive as long as @out is alive. + * + * If it's not partial, perhaps a file we write to, or a memory image, + * it's fine for @image to go away. + */ + if( vips_image_ispartial( out ) ) { + g_object_ref( image ); + vips_object_local( out, image ); + } + return( 0 ); }