graphics: Bug fix: Don't touch pixelsToSet when disposing a subimage

This change also removes finalizer for subimages since this is not
necessary.

Fixes #823
This commit is contained in:
Hajime Hoshi 2019-02-21 23:07:09 +09:00
parent dbe4f27d5f
commit 8934f8296f
2 changed files with 23 additions and 3 deletions

View File

@ -505,9 +505,7 @@ func (i *Image) SubImage(r image.Rectangle) image.Image {
} else { } else {
img.original = i img.original = i
} }
img.addr = img img.addr = img
runtime.SetFinalizer(img, (*Image).Dispose)
r = r.Intersect(img.Bounds()) r = r.Intersect(img.Bounds())
// Need to check Empty explicitly. See the standard image package implementations. // Need to check Empty explicitly. See the standard image package implementations.
@ -637,8 +635,8 @@ func (i *Image) Dispose() error {
} }
if !i.isSubimage() { if !i.isSubimage() {
i.mipmap.dispose() i.mipmap.dispose()
}
i.resolvePixelsToSet(false) i.resolvePixelsToSet(false)
}
runtime.SetFinalizer(i, nil) runtime.SetFinalizer(i, nil)
return nil return nil
} }

View File

@ -23,6 +23,7 @@ import (
_ "image/png" _ "image/png"
"math" "math"
"os" "os"
"runtime"
"testing" "testing"
. "github.com/hajimehoshi/ebiten" . "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)
}
}