@@ -12,16 +12,6 @@ class Source extends Connection
1212 */
1313 public \FFI \CData $ pointer ;
1414
15- /**
16- * Pointer to the underlying memory buffer when using
17- * @see Source::newFromMemory()
18- *
19- * Must be freed when no longer needed.
20- *
21- * @internal
22- */
23- public ?\FFI \CData $ memory = null ;
24-
2515 public function __construct (\FFI \CData $ pointer )
2616 {
2717 $ this ->pointer = FFI ::vips ()->cast (FFI ::ctypes ('VipsSource ' ), $ pointer );
@@ -74,27 +64,19 @@ public static function newFromFile(string $filename): self
7464 */
7565 public static function newFromMemory (string $ data ): self
7666 {
77- # we need to set the memory to a copy of the data
78- $ n = strlen ($ data );
79- $ memory = FFI ::vips ()->new ("char[ $ n] " , false , true );
80- \FFI ::memcpy ($ memory , $ data , $ n );
81- $ pointer = FFI ::vips ()->vips_source_new_from_memory ($ memory , $ n );
67+ $ blob = FFI ::vips ()->vips_blob_copy ($ data , strlen ($ data ));
68+ if ($ blob === null ) {
69+ throw new Exception ("can't create source from memory " );
70+ }
8271
72+ $ pointer = FFI ::vips ()->vips_source_new_from_blob ($ blob );
8373 if ($ pointer === null ) {
84- \ FFI ::free ( $ memory );
74+ FFI ::vips ()-> vips_area_unref ( $ blob );
8575 throw new Exception ("can't create source from memory " );
8676 }
8777
8878 $ source = new self ($ pointer );
89- $ source -> memory = $ memory ;
79+ FFI :: vips ()-> vips_area_unref ( $ blob ) ;
9080 return $ source ;
9181 }
92-
93- public function __destruct ()
94- {
95- if ($ this ->memory !== null ) {
96- \FFI ::free ($ this ->memory );
97- }
98- parent ::__destruct ();
99- }
10082}
0 commit comments