Closed galsjel closed 3 months ago
Thanks! More minimized case is:
package main
import (
"image"
"runtime"
"github.com/hajimehoshi/ebiten/v2"
)
type Game struct {
img *ebiten.Image
bytes []byte
}
func (g *Game) Update() error {
if g.img == nil {
g.img = ebiten.NewImageWithOptions(image.Rect(0, 0, 128, 128), &ebiten.NewImageOptions{
Unmanaged: true,
})
}
if g.bytes == nil {
g.bytes = make([]byte, 128*128*4)
}
g.img.WritePixels(g.bytes)
var m runtime.MemStats
runtime.ReadMemStats(&m)
println(m.HeapAlloc)
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return 320, 240
}
func main() {
if err := ebiten.RunGame(&Game{}); err != nil {
panic(err)
}
}
(*graphicscommand.Image).flushBufferedWritePixels
is called for managed images, but not for unmanaged images. This is mysterious.
EDIT: OK, IIUC, a managed image shares the same atlas with an internal image that is cleared every frame.
Please try 71b7cedc5b9907f4b0e8838e71688c9895d0997c (2.7) or 9bc5ed38479b9193961a339ff4b3a59cf243b16c (main), thanks
This happens only when 1. WritePixels is called on an unmanaged image and 2. the image is never used as a source (or a destination). So this should be rare in the real world situation. For 1., it can happen even with a managed image when the image is in an isolated atlas, but this is very very unlikely.
Ebitengine Version
v2.7.0-alpha.2.0.20231008182829-7e17b25c5666
Operating System
Go Version (
go version
)go version go1.22.4 windows/amd64
What steps will reproduce the problem?
or
What is the expected result?
Heap allocation to remain relatively stable whether an image is managed or not.
What happens instead?
Heap allocation increases with each
WritePixels
call on an unmanaged image.Anything else you feel useful to add?
Setting
Unmanaged: false
does not leak. This problem appears to have existed since byte pooling was introduced tointernal/atlas
. This can be observed by comparing results against the commit prior to the one above.No leak: 2405b7e825c63e02694c516c975d305b92a990b1 Leaking: 7e17b25c56660785a06e263bdfe82fea69004216