Fix memory leak in animation encoding (#1)

We were never freeing the buffer that holds the assembled WebP
animation.

Instead, copy the buffer into Go and free it right after assembly.
master
Rohan Singh 3 years ago committed by GitHub
parent 6042ce446e
commit 1fabc3b466
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -107,15 +107,26 @@ func (ae *AnimationEncoder) Assemble() ([]byte, error) {
data := &C.WebPData{} data := &C.WebPData{}
C.WebPDataInit(data) C.WebPDataInit(data)
defer C.WebPDataClear(data)
if C.WebPAnimEncoderAssemble(ae.c, data) == 0 { if C.WebPAnimEncoderAssemble(ae.c, data) == 0 {
return nil, errors.New("Error assembling animation") return nil, errors.New("Error assembling animation")
} }
return C.GoBytes( size := int(data.size)
out := make([]byte, size)
n := copy(
out, C.GoBytes(
unsafe.Pointer(data.bytes), unsafe.Pointer(data.bytes),
C.int(int(data.size)), C.int(size),
), nil ),
)
if n != size {
return nil, errors.New("Error copying animation from C to Go")
}
return out, nil
} }
// Close deletes the encoder and frees resources. // Close deletes the encoder and frees resources.

Loading…
Cancel
Save