diff --git a/image.go b/image.go index cf31e1df1..b9973d0de 100644 --- a/image.go +++ b/image.go @@ -505,9 +505,7 @@ func (i *Image) SubImage(r image.Rectangle) image.Image { } else { img.original = i } - img.addr = img - runtime.SetFinalizer(img, (*Image).Dispose) r = r.Intersect(img.Bounds()) // Need to check Empty explicitly. See the standard image package implementations. @@ -637,8 +635,8 @@ func (i *Image) Dispose() error { } if !i.isSubimage() { i.mipmap.dispose() + i.resolvePixelsToSet(false) } - i.resolvePixelsToSet(false) runtime.SetFinalizer(i, nil) return nil } diff --git a/image_test.go b/image_test.go index 9792ef5ba..61b3b2606 100644 --- a/image_test.go +++ b/image_test.go @@ -23,6 +23,7 @@ import ( _ "image/png" "math" "os" + "runtime" "testing" . "github.com/hajimehoshi/ebiten" @@ -1595,3 +1596,24 @@ func TestImageDrawTrianglesWithSubImage(t *testing.T) { } } } + +// Issue #823 +func TestImageAtAfterDisposingSubImage(t *testing.T) { + img, _ := NewImage(16, 16, FilterDefault) + img.Set(0, 0, color.White) + img.SubImage(image.Rect(0, 0, 16, 16)) + runtime.GC() + got := img.At(0, 0) + want := color.RGBA{0xff, 0xff, 0xff, 0xff} + if got != want { + t.Errorf("got: %v, want: %v", got, want) + } + + img.Set(0, 1, color.White) + sub := img.SubImage(image.Rect(0, 0, 16, 16)).(*Image) + sub.Dispose() + got = img.At(0, 1) + if got != want { + t.Errorf("got: %v, want: %v", got, want) + } +}