From 88cfd219e7e16306ac571bdb5d06abb5d37ae4e2 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 27 Mar 2018 10:58:13 +0900 Subject: [PATCH] mp3: Set finalizer for the stream --- audio/mp3/decode_notjs.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/audio/mp3/decode_notjs.go b/audio/mp3/decode_notjs.go index 1450bb76a..27b949222 100644 --- a/audio/mp3/decode_notjs.go +++ b/audio/mp3/decode_notjs.go @@ -21,6 +21,8 @@ package mp3 import ( + "runtime" + "github.com/hajimehoshi/go-mp3" "github.com/hajimehoshi/ebiten/audio" @@ -51,6 +53,7 @@ func (s *Stream) Seek(offset int64, whence int) (int64, error) { // Close is implementation of io.Closer's Close. func (s *Stream) Close() error { + runtime.SetFinalizer(s, nil) if s.resampling != nil { return s.resampling.Close() } @@ -84,8 +87,10 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) { if d.SampleRate() != context.SampleRate() { r = convert.NewResampling(d, d.Length(), d.SampleRate(), context.SampleRate()) } - return &Stream{ + s := &Stream{ orig: d, resampling: r, - }, nil + } + runtime.SetFinalizer(s, (*Stream).Close) + return s, nil }