diff --git a/CHANGELOG.md b/CHANGELOG.md index dcbe232..ed20e28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## master * raise Vips::Error if `copy_memory` fails [VitaliiLazebnyi] +* `Source.new_from_memory` keeps a reference to the memory area [jeremy] ## Version 2.3.0 (2025-12-10) diff --git a/lib/vips/image.rb b/lib/vips/image.rb index 1a4347b..af2bd0f 100644 --- a/lib/vips/image.rb +++ b/lib/vips/image.rb @@ -1017,10 +1017,10 @@ def add_alpha # # @return [Image] new memory image def copy_memory - new_image = Vips.vips_image_copy_memory self - raise Vips::Error if new_image.null? + ptr = Vips.vips_image_copy_memory self + raise Vips::Error if ptr.null? - Vips::Image.new new_image + Vips::Image.new ptr end # Draw a point on an image. @@ -1037,8 +1037,7 @@ def draw_point ink, left, top, **opts # @param other [Image, Real, Array] Thing to add to self # @return [Image] result of addition def + other - other.is_a?(Vips::Image) ? - add(other) : linear(1, other) + other.is_a?(Vips::Image) ? add(other) : linear(1, other) end # Subtract an image, constant or array. @@ -1055,8 +1054,7 @@ def - other # @param other [Image, Real, Array] Thing to multiply by self # @return [Image] result of multiplication def * other - other.is_a?(Vips::Image) ? - multiply(other) : linear(other, 0) + other.is_a?(Vips::Image) ? multiply(other) : linear(other, 0) end # Divide an image, constant or array. @@ -1073,8 +1071,7 @@ def / other # @param other [Image, Real, Array] self modulo this # @return [Image] result of modulo def % other - other.is_a?(Vips::Image) ? - remainder(other) : remainder_const(other) + other.is_a?(Vips::Image) ? remainder(other) : remainder_const(other) end # Raise to power of an image, constant or array. diff --git a/lib/vips/source.rb b/lib/vips/source.rb index a252697..8549fb6 100644 --- a/lib/vips/source.rb +++ b/lib/vips/source.rb @@ -78,11 +78,14 @@ def self.new_from_file(filename) def self.new_from_memory(data) ptr = Vips.vips_source_new_from_memory data, data.bytesize raise Vips::Error if ptr.null? + source = Vips::Source.new ptr - # FIXME do we need to keep a ref to the underlying memory area? what - # about Image.new_from_buffer? Does that need a secret ref too? + # keep a secret ref to the underlying object .. this reference will be + # inherited by things that in turn depend on us, so the memory we are + # using will not be freed + source.references << data - Vips::Source.new ptr + source end end end