nativeaudio

audio playback for Go
Log | Files | Refs | README | LICENSE

commit 4ac2f1f7ae4d7b0f02ceb52b15f9b5d1c724a7c4
parent 2fb440b183ccd78f46e865faca1504d45d6d09d0
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date:   Mon, 23 Oct 2023 08:46:40 +0800

windows: fix memory leak

We need to free the data which is allocated on the C heap returned from
C.CBytes function.

Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>

Diffstat:
Maudio_windows.go | 8+++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/audio_windows.go b/audio_windows.go @@ -76,18 +76,24 @@ func load(path string) (uncompressed []byte, format Format, err error) { // // uncompressed is a read-only slice backed by a C buffer. Do not mutate. func decode(compressed []byte) (uncompressed []byte, format Format, err error) { - r := C.Decode((*C.uchar)(C.CBytes(compressed)), C.uint(len(compressed))) + data := C.CBytes(compressed) + defer C.free(data) + + r := C.Decode((*C.uchar)(data), C.uint(len(compressed))) if r.Err != nil && r.Err.Str != nil { defer C.ErrorFree(r.Err) return nil, format, collectErrors(r.Err) } + defer C.BufferFree(r.Uncompressed) + uncompressed = C.GoBytes(unsafe.Pointer(r.Uncompressed.Data), C.int(r.Uncompressed.Len)) format = Format{ Channels: int(r.Format.Channels), BitDepth: int(r.Format.BitDepth), SampleRate: int(r.Format.SampleRate), } + return uncompressed, format, nil }