graphics: Initialize (*Image).addr asap not to rely on the fact that fill is called

This commit is contained in:
Hajime Hoshi 2018-04-06 00:35:18 +09:00
parent 8ff79c970a
commit abdfb3b713

View File

@ -57,12 +57,6 @@ type Image struct {
} }
func (i *Image) copyCheck() { func (i *Image) copyCheck() {
if i.addr == nil {
// As it is OK that an image is allocated at heap,
// 'noespace' function like strings.noescape is not needed.
i.addr = i
return
}
if i.addr != i { if i.addr != i {
panic("ebiten: illegal use of non-zero Image copied by value") panic("ebiten: illegal use of non-zero Image copied by value")
} }
@ -365,6 +359,7 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
shareableImage: s, shareableImage: s,
filter: filter, filter: filter,
} }
i.addr = i
i.fill(0, 0, 0, 0) i.fill(0, 0, 0, 0)
runtime.SetFinalizer(i, (*Image).Dispose) runtime.SetFinalizer(i, (*Image).Dispose)
return i, nil return i, nil
@ -377,6 +372,7 @@ func newImageWithoutInit(width, height int) *Image {
shareableImage: s, shareableImage: s,
filter: FilterDefault, filter: FilterDefault,
} }
i.addr = i
runtime.SetFinalizer(i, (*Image).Dispose) runtime.SetFinalizer(i, (*Image).Dispose)
return i return i
} }
@ -401,6 +397,7 @@ func newVolatileImage(width, height int, filter Filter) *Image {
shareableImage: shareable.NewVolatileImage(width, height), shareableImage: shareable.NewVolatileImage(width, height),
filter: filter, filter: filter,
} }
i.addr = i
i.fill(0, 0, 0, 0) i.fill(0, 0, 0, 0)
runtime.SetFinalizer(i, (*Image).Dispose) runtime.SetFinalizer(i, (*Image).Dispose)
return i return i
@ -424,6 +421,7 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
shareableImage: s, shareableImage: s,
filter: filter, filter: filter,
} }
i.addr = i
runtime.SetFinalizer(i, (*Image).Dispose) runtime.SetFinalizer(i, (*Image).Dispose)
_ = i.ReplacePixels(graphicsutil.CopyImage(source)) _ = i.ReplacePixels(graphicsutil.CopyImage(source))
@ -435,6 +433,7 @@ func newImageWithScreenFramebuffer(width, height int) *Image {
shareableImage: shareable.NewScreenFramebufferImage(width, height), shareableImage: shareable.NewScreenFramebufferImage(width, height),
filter: FilterDefault, filter: FilterDefault,
} }
i.addr = i
runtime.SetFinalizer(i, (*Image).Dispose) runtime.SetFinalizer(i, (*Image).Dispose)
return i return i
} }