wav: Set finalizer for the stream

This commit is contained in:
Hajime Hoshi 2018-03-27 12:10:46 +09:00
parent 88cfd219e7
commit f1f0277deb

View File

@ -19,6 +19,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"runtime"
"github.com/hajimehoshi/ebiten/audio" "github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/internal/convert" "github.com/hajimehoshi/ebiten/audio/internal/convert"
@ -44,6 +45,7 @@ func (s *Stream) Seek(offset int64, whence int) (int64, error) {
// Read is implementation of io.Closer's Close. // Read is implementation of io.Closer's Close.
func (s *Stream) Close() error { func (s *Stream) Close() error {
runtime.SetFinalizer(s, nil)
return s.inner.Close() return s.inner.Close()
} }
@ -105,6 +107,7 @@ func (s *stream) Seek(offset int64, whence int) (int64, error) {
// Close is implementation of io.Closer's Close. // Close is implementation of io.Closer's Close.
func (s *stream) Close() error { func (s *stream) Close() error {
runtime.SetFinalizer(s, nil)
return s.src.Close() return s.src.Close()
} }
@ -214,6 +217,8 @@ chunks:
dataSize: dataSize, dataSize: dataSize,
remaining: dataSize, remaining: dataSize,
} }
runtime.SetFinalizer(s, (*stream).Close)
if mono || bitsPerSample != 16 { if mono || bitsPerSample != 16 {
s = convert.NewStereo16(s, mono, bitsPerSample != 16) s = convert.NewStereo16(s, mono, bitsPerSample != 16)
if mono { if mono {
@ -228,5 +233,7 @@ chunks:
s = r s = r
dataSize = r.Length() dataSize = r.Length()
} }
return &Stream{inner: s, size: dataSize}, nil ss := &Stream{inner: s, size: dataSize}
runtime.SetFinalizer(ss, (*Stream).Close)
return ss, nil
} }