nativeaudio

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

commit 08f125a1e34fc4d76e971f7132364b310bf09acd
parent 9e0bc636f05197087bd5e5b3c0b7de3cfb8d1e64
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date:   Mon, 25 Oct 2021 03:15:58 +0800

nativeaudio: [fix] properly free dynamic errors

Walk the chain and deallocate as we go.

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

Diffstat:
Maudio_windows.c | 18++++++++++++++++++
Maudio_windows.go | 2+-
Maudio_windows.h | 5+++++
3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/audio_windows.c b/audio_windows.c @@ -27,6 +27,24 @@ ErrorWithCode(Error *err, int code) return err; } +// ErrorFree deallocates the error and any wrapped errors. +void +ErrorFree(Error* err) +{ + // cursor points to the error currently being processed. + Error *cursor = NULL; + + while (err != NULL) + { + cursor = err; + err = err->Err; + if (cursor->Str != NULL) + free(cursor->Str); + free(cursor); + } +} + + // NewResult constructs a Result with the provided value and error. // Usually one of the hose pointers will be NULL. Result diff --git a/audio_windows.go b/audio_windows.go @@ -36,7 +36,7 @@ func load(path string) ([]byte, error) { defer C.free(unsafe.Pointer(cPath)) result := C.Load(cPath) if result.Err != nil { - defer C.free(unsafe.Pointer(result.Err)) + defer C.ErrorFree(result.Err) return nil, fmt.Errorf(C.GoString(result.Err.Str)) } buffer := (*C.Buffer)(result.Value) diff --git a/audio_windows.h b/audio_windows.h @@ -44,8 +44,13 @@ typedef struct Buffer BYTE* Data; // Data is the pointer to the first byte. } Buffer; +// BufferFree deallocates the memory for a buffer, including the pointer +// to it and it's pointer to the raw data. void BufferFree(Buffer*); +// ErrorFree deallocates the memory for an error and all wrapped errors. +void ErrorFree(Error*); + // Load the decoded PCM data from the given file. // // Load is implemented over the top of Windows Media Foundation and