shareable: Bug fix: backend's restorable image must be disposed

This commit is contained in:
Hajime Hoshi 2018-04-06 01:12:08 +09:00
parent 87daa82ad9
commit 1a335032a2
2 changed files with 34 additions and 2 deletions

View File

@ -191,6 +191,7 @@ func (s *Image) dispose() {
return return
} }
s.backend.restorable.Dispose()
index := -1 index := -1
for i, sh := range theBackends { for i, sh := range theBackends {
if sh == s.backend { if sh == s.backend {
@ -313,3 +314,9 @@ func Restore() error {
defer backendsM.Unlock() defer backendsM.Unlock()
return restorable.Restore() return restorable.Restore()
} }
func BackendNumForTesting() int {
backendsM.Lock()
defer backendsM.Unlock()
return len(theBackends)
}

View File

@ -40,13 +40,15 @@ func TestMain(m *testing.M) {
os.Exit(code) os.Exit(code)
} }
const bigSize = 2049
func TestEnsureNotShared(t *testing.T) { func TestEnsureNotShared(t *testing.T) {
// Create img1 and img2 with this size so that the next images are allocated // Create img1 and img2 with this size so that the next images are allocated
// with non-upper-left location. // with non-upper-left location.
img1 := NewImage(2049, 100) img1 := NewImage(bigSize, 100)
defer img1.Dispose() defer img1.Dispose()
img2 := NewImage(100, 2049) img2 := NewImage(100, bigSize)
defer img2.Dispose() defer img2.Dispose()
const size = 32 const size = 32
@ -97,3 +99,26 @@ func TestEnsureNotShared(t *testing.T) {
} }
} }
} }
func TestDispose(t *testing.T) {
// There are already backend image for the offscreen or something.
// Creating a big image and the next image should be created at a new backend.
img1 := NewImage(bigSize, bigSize)
defer img1.Dispose()
if BackendNumForTesting() != 1 {
t.Errorf("BackendNumForTesting(): got: %d, want: %d", BackendNumForTesting(), 1)
}
img2 := NewImage(bigSize, bigSize)
if BackendNumForTesting() != 2 {
t.Errorf("BackendNumForTesting(): got: %d, want: %d", BackendNumForTesting(), 2)
}
img2.Dispose()
if BackendNumForTesting() != 1 {
t.Errorf("BackendNumForTesting(): got: %d, want: %d", BackendNumForTesting(), 1)
}
}