[bug ]The application freezes and does not respond tightly
What happend?
When you start the application, it freezes, it may start after 3,5,6 times, or it may not start. I don't understand what the problem is. The issue tends to reproduce approximately once in every 30-50 launches. Please attempt multiple restarts to observe the problem, as it may not occur within the first few attempts.
Code example
main.go
var (
key string
)
func GoGetCenterScreen(width, height int) (int, int) {
centerX := width / 2
centerY := height / 2
return centerX, centerY
}
func loop() {
giu.PushStyleColor(giu.StyleColorWindowBg, giu.Vec4ToRGBA(imgui.Vec4{X: 0, Y: 0, Z: 0, W: 1.00}))
imgui.PushStyleColorVec4(imgui.ColWindowBg, imgui.Vec4{X: 0, Y: 0, Z: 0, W: 1.00})
giu.SingleWindow().Layout(
giu.PrepareMsgbox(),
giu.Align(giu.AlignCenter).To(
giu.Label("Please, enter the key:"),
giu.InputText(&key).Size(400),
giu.Button("Авторизоваться").OnClick(func() {
if key == "" {
giu.Msgbox("Ошибка", "Ключ не может быть пустым.")
return
}
fmt.Println("Ключ:", key)
}),
),
)
imgui.PopStyleColor()
giu.PopStyleColor()
}
func loadIcon(filePath string) (image.Image, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
icon, err := png.Decode(file)
if err != nil {
return nil, err
}
return icon, nil
}
func main() {
screenWidth, screenHeight := robotgo.GetScreenSize()
centerX, centerY := GoGetCenterScreen(screenWidth, screenHeight)
wnd := giu.NewMasterWindow("Loader", 500, 400, giu.MasterWindowFlagsNotResizable)
wnd.SetPos(centerX-250, centerY-200)
icon, err := loadIcon("C:/Users/skyar/OneDrive/Рабочий стол/loader/src/59365.png")
if err == nil {
wnd.SetIcon(icon)
} else {
fmt.Println("Ошибка загрузки иконки:", err)
}
wnd.Run(loop)
}
To Reproduce
- Run my demo
- will see the crash...
Version
(latest)
OS
Windows 11
Is this on windows? is so, is your icon if dimension 2^n ?
I think I am facing similar issue of occasion freeze on start. It is very random, and even happens when having just a master window and an empty run loop. I tried using older version of giu, and it seems to start happening from v0.8.0, v0.7.0 seems not affected.
I found a fix for the application freezing issue. It's necessary to add runtime.LockOSThread() and runtime.UnlockOSThread() in the main function to ensure proper access to the GUI from a single thread. This should resolve the freezing during startup.
Fixed code
main.go
package main
import (
"fmt"
"runtime"
g "github.com/AllenDang/giu"
)
func onClickMe() {
fmt.Println("Hello world!")
}
func onImSoCute() {
fmt.Println("Im sooooooo cute!!")
}
func loop() {
g.SingleWindow().Layout(
g.Label("Hello world from giu"),
g.Row(
g.Button("Click Me").OnClick(onClickMe),
g.Button("I'm so cute").OnClick(onImSoCute),
),
)
}
func main() {
runtime.LockOSThread()
wnd := g.NewMasterWindow("Hello world", 400, 200, g.MasterWindowFlagsNotResizable)
wnd.Run(loop)
runtime.UnlockOSThread()
}
Version
(latest)
ref: #764
Could you guys try this?
diff --git a/mainthread_windows.go b/mainthread_windows.go
index e38db0d..4eade0a 100755
--- a/mainthread_windows.go
+++ b/mainthread_windows.go
@@ -3,10 +3,16 @@
package giu
+import "runtime"
+
+func init() {
+ runtime.LockOSThread()
+}
+
// TODO: I have no working mainthread library for windows.
// - this one for macOS crashes app immediately
// - this for linux (and everything else) freezes after a few seconds
-//
+//
// With no mianthread support this at least runs sometimes. Just keep your giu calls in one thread and everything should work.
func mainthreadCallPlatform(c func()) {
c()
CC @kenn1dy @SleepyPrince
@gucio321 Yeah, that diff has fixed the same issue for me as well (Windows 11, latest giu/cimgui-go master), thanks a lot!
ok, so let me apply that diff to master and close this and #764