From c411ca492fcb95a19ebd62e3d8e23aa1c5de0934 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 11 Mar 2018 00:07:32 +0900 Subject: [PATCH] shareable: Set finalizers --- internal/shareable/shareable.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/shareable/shareable.go b/internal/shareable/shareable.go index 477330241..ff48f9a2f 100644 --- a/internal/shareable/shareable.go +++ b/internal/shareable/shareable.go @@ -17,6 +17,7 @@ package shareable import ( "fmt" "image/color" + "runtime" "github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/graphics" @@ -116,6 +117,7 @@ func (s *Image) Dispose() { defer func() { s.backend = nil s.node = nil + runtime.SetFinalizer(s, nil) }() if s.node == nil { @@ -180,26 +182,32 @@ func NewImage(width, height int) *Image { if n == nil { panic("not reached") } - return &Image{ + i := &Image{ backend: s, node: n, } + runtime.SetFinalizer(i, (*Image).Dispose) + return i } func NewVolatileImage(width, height int) *Image { r := restorable.NewImage(width, height, true) - return &Image{ + i := &Image{ backend: &backend{ restorable: r, }, } + runtime.SetFinalizer(i, (*Image).Dispose) + return i } func NewScreenFramebufferImage(width, height int) *Image { r := restorable.NewScreenFramebufferImage(width, height) - return &Image{ + i := &Image{ backend: &backend{ restorable: r, }, } + runtime.SetFinalizer(i, (*Image).Dispose) + return i }