Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug ]The application freezes and does not respond tightly #881

Closed
lchpazik opened this issue Oct 11, 2024 · 8 comments · Fixed by #887
Closed

[bug ]The application freezes and does not respond tightly #881

lchpazik opened this issue Oct 11, 2024 · 8 comments · Fixed by #887
Labels
bug Something isn't working
Milestone

Comments

@lchpazik
Copy link

lchpazik commented Oct 11, 2024

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.

935868 717_image

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

  1. Run my demo
  2. will see the crash...

Version

(latest)

OS

Windows 11

@lchpazik lchpazik added the bug Something isn't working label Oct 11, 2024
@cjbrigato
Copy link
Contributor

cjbrigato commented Oct 12, 2024

Is this on windows? is so, is your icon if dimension 2^n ?

@SleepyPrince
Copy link
Contributor

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.

@lchpazik
Copy link
Author

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)

@gucio321
Copy link
Collaborator

ref: #764

@gucio321
Copy link
Collaborator

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()

@gucio321 gucio321 added this to the v0.9.1 milestone Oct 13, 2024
@gucio321
Copy link
Collaborator

CC @kenn1dy @SleepyPrince

@ioxenus
Copy link

ioxenus commented Oct 19, 2024

@gucio321 Yeah, that diff has fixed the same issue for me as well (Windows 11, latest giu/cimgui-go master), thanks a lot!

@gucio321
Copy link
Collaborator

ok, so let me apply that diff to master and close this and #764

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants