shareable: Set finalizers

This commit is contained in:
Hajime Hoshi 2018-03-11 00:07:32 +09:00
parent a2d6ae7eee
commit c411ca492f

View File

@ -17,6 +17,7 @@ package shareable
import ( import (
"fmt" "fmt"
"image/color" "image/color"
"runtime"
"github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
@ -116,6 +117,7 @@ func (s *Image) Dispose() {
defer func() { defer func() {
s.backend = nil s.backend = nil
s.node = nil s.node = nil
runtime.SetFinalizer(s, nil)
}() }()
if s.node == nil { if s.node == nil {
@ -180,26 +182,32 @@ func NewImage(width, height int) *Image {
if n == nil { if n == nil {
panic("not reached") panic("not reached")
} }
return &Image{ i := &Image{
backend: s, backend: s,
node: n, node: n,
} }
runtime.SetFinalizer(i, (*Image).Dispose)
return i
} }
func NewVolatileImage(width, height int) *Image { func NewVolatileImage(width, height int) *Image {
r := restorable.NewImage(width, height, true) r := restorable.NewImage(width, height, true)
return &Image{ i := &Image{
backend: &backend{ backend: &backend{
restorable: r, restorable: r,
}, },
} }
runtime.SetFinalizer(i, (*Image).Dispose)
return i
} }
func NewScreenFramebufferImage(width, height int) *Image { func NewScreenFramebufferImage(width, height int) *Image {
r := restorable.NewScreenFramebufferImage(width, height) r := restorable.NewScreenFramebufferImage(width, height)
return &Image{ i := &Image{
backend: &backend{ backend: &backend{
restorable: r, restorable: r,
}, },
} }
runtime.SetFinalizer(i, (*Image).Dispose)
return i
} }