Skip to content

Commit 34d1e6e

Browse files
authored
Merge pull request #354 from gucio321/texture-releasing
backend/texture: change default texture impl
2 parents 1d3efee + 07c7063 commit 34d1e6e

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

backend/texture.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ import (
88
_ "image/png"
99
"os"
1010
"path/filepath"
11-
"runtime"
1211

1312
"github.com/AllenDang/cimgui-go/imgui"
1413
)
1514

15+
// Texture implements a simple texture loader. It wraps backend's methods to allow creating textures easily.
16+
// IMPORTANT: as the texture is mainly handled by C OpenGL, it is not covered by Garbae Collector (GC).
17+
//
18+
// Remember to call (*Texture).Release when you no longer need it.
1619
type Texture struct {
1720
ID imgui.TextureID
1821
Width int
@@ -28,13 +31,16 @@ func NewTextureFromRgba(rgba *image.RGBA) *Texture {
2831
Height: rgba.Bounds().Dy(),
2932
}
3033

31-
// Set finalizer
32-
runtime.SetFinalizer(&texture, (*Texture).release)
34+
// I leav it for documentation here:
35+
// GC runs in a separated thread so this may not work correctly (will crash opengl)
36+
// runtime.SetFinalizer(&texture, (*Texture).release)
3337

3438
return &texture
3539
}
3640

37-
func (t *Texture) release() {
41+
// Release tells OpenGL that this texture is no longer needed.
42+
// ATTENTION: This will not be automatically handled by GC so remember to do it manually if you have many textures!
43+
func (t *Texture) Release() {
3844
textureManager.DeleteTexture(t.ID)
3945
}
4046

0 commit comments

Comments
 (0)