From 82918d41e2a2a4235a538dd3bcc1bdac1a21dba4 Mon Sep 17 00:00:00 2001 From: Paul van Santen Date: Sun, 13 Dec 2020 19:25:47 +0100 Subject: [PATCH] Revert "Avoid allocating a new go byte slice buffer and instead reuse the C buffer" This reverts commit bf2aa07cac26c4995a3bdfcf6e5c61adefedc98a. --- vips/foreign.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/vips/foreign.go b/vips/foreign.go index 80f8d12b..9f502a48 100644 --- a/vips/foreign.go +++ b/vips/foreign.go @@ -9,7 +9,6 @@ import ( "fmt" "image/png" "math" - "reflect" "runtime" "unsafe" @@ -303,12 +302,8 @@ func vipsSaveJPEGToBuffer(in *C.VipsImage, quality int, stripMetadata, interlace } func toBuff(ptr unsafe.Pointer, cLen C.size_t) []byte { - // Create a go slice from the C memory without copying the data - // https://stackoverflow.com/questions/51187973/how-to-create-an-array-or-a-slice-from-an-array-unsafe-pointer-in-golang - sh := &reflect.SliceHeader{ - Data: uintptr(ptr), - Len: int(cLen), - Cap: int(cLen), - } - return *(*[]byte)(unsafe.Pointer(sh)) + buf := C.GoBytes(ptr, C.int(cLen)) + gFreePointer(ptr) + + return buf }