diff --git a/libvips/iofuncs/image.c b/libvips/iofuncs/image.c index 79407620..0f40aa79 100644 --- a/libvips/iofuncs/image.c +++ b/libvips/iofuncs/image.c @@ -1651,6 +1651,14 @@ vips_image_write( VipsImage *image, VipsImage *out ) vips_demand_hint( out, VIPS_DEMAND_STYLE_THINSTRIP, image, NULL ); + /* If this will be a delayed calculation we need to keep @image + * around for as long as @out is about. + */ + if( vips_image_ispartial( image ) ) { + 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 ) ) diff --git a/libvips/iofuncs/region.c b/libvips/iofuncs/region.c index c7e88bc8..1134ab48 100644 --- a/libvips/iofuncs/region.c +++ b/libvips/iofuncs/region.c @@ -282,6 +282,7 @@ vips_region_dispose( GObject *gobject ) image->regions = g_slist_remove( image->regions, region ); g_mutex_unlock( image->sslock ); region->im = NULL; + g_object_unref( image ); G_OBJECT_CLASS( vips_region_parent_class )->dispose( gobject ); @@ -461,20 +462,26 @@ vips_region_new( VipsImage *image ) { VipsRegion *region; - region = VIPS_REGION( g_object_new( VIPS_TYPE_REGION, NULL ) ); - - /* We can't use the property system, we need to be very threaded. + /* Ref quickly, we want to make sure we keep the image around. + * We can't use the property system, we need to be very threaded. */ - region->im = image; g_object_ref( image ); + g_assert( G_OBJECT( image )->ref_count > 1 ); + +#ifdef DEBUG + g_assert( vips_object_sanity( VIPS_OBJECT( image ) ) ); +#endif /*DEBUG*/ + + region = VIPS_REGION( g_object_new( VIPS_TYPE_REGION, NULL ) ); + region->im = image; + if( vips_object_build( VIPS_OBJECT( region ) ) ) { VIPS_UNREF( region ); return( NULL ); } #ifdef DEBUG - g_assert( vips_object_sanity( VIPS_OBJECT( image ) ) ); g_assert( vips_object_sanity( VIPS_OBJECT( region ) ) ); #endif /*DEBUG*/