AllenDang / giu

Cross platform rapid GUI framework for golang based on Dear ImGui.
MIT License
2.16k stars 128 forks source link

[bug] examples/widgets broken on cimgui-go #700

Closed AllenDang closed 8 months ago

AllenDang commented 9 months ago

What happend?

Failed to start with following error log.

2023-09-22 21:26:48.585 widgets[70477:6681227] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'nextEventMatchingMask should only be called from the Main Thread!'
*** First throw call stack:
(
        0   CoreFoundation                      0x00000001824fb154 __exceptionPreprocess + 176
        1   libobjc.A.dylib                     0x000000018201a4d4 objc_exception_throw + 60
        2   AppKit                              0x00000001856a7368 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2844
        3   widgets                             0x00000001050c9348 _glfwPlatformPollEvents + 152
        4   widgets                             0x00000001050c1b80 glfwPollEvents + 48
        5   widgets                             0x0000000104e58c88 igRunLoop + 252
        6   widgets                             0x0000000104d3646c runtime.asmcgocall.abi0 + 124
)
libc++abi: terminating due to uncaught exception of type NSException
SIGABRT: abort
PC=0x18236c764 m=5 sigcode=0
signal arrived during cgo execution

goroutine 35 [syscall]:
runtime.cgocall(0x104e4d6e4, 0x14000235ee8)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/cgocall.go:157 +0x44 fp=0x14000235eb0 sp=0x14000235e70 pc=0x104cd3cb4
github.com/AllenDang/cimgui-go._Cfunc_igRunLoop(0x142e4d090, 0x104e113f4, 0x104e11440, 0x104e1148c, 0x104e11524)
        _cgo_gotypes.go:29012 +0x30 fp=0x14000235ee0 sp=0x14000235eb0 pc=0x104d8fcd0
github.com/AllenDang/cimgui-go.(*GLFWBackend).Run.func1(0x1?)
        /Users/allen/go/pkg/mod/github.com/!allen!dang/cimgui-go@v0.0.0-20230918114712-675654f053d9/glfw_backend.go:251 +0xa4 fp=0x14000235f50 sp=0x14000235ee0 pc=0x104d98ac4
github.com/AllenDang/cimgui-go.(*GLFWBackend).Run(0x0?, 0x0?)
        /Users/allen/go/pkg/mod/github.com/!allen!dang/cimgui-go@v0.0.0-20230918114712-675654f053d9/glfw_backend.go:251 +0x40 fp=0x14000235f70 sp=0x14000235f50 pc=0x104d989f0
main.main.(*MasterWindow).Run.func1()
        /Users/allen/Documents/Gocode/src/github.com/AllenDang/giu/MasterWindow.go:241 +0xe4 fp=0x14000235fa0 sp=0x14000235f70 pc=0x104e10894
github.com/faiface/mainthread.Run.func1()
        /Users/allen/go/pkg/mod/github.com/faiface/mainthread@v0.0.0-20171120011319-8b78f0a41ae3/mainthread.go:37 +0x30 fp=0x14000235fd0 sp=0x14000235fa0 pc=0x104dfe100
runtime.goexit()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/asm_arm64.s:1197 +0x4 fp=0x14000235fd0 sp=0x14000235fd0 pc=0x104d36674
created by github.com/faiface/mainthread.Run in goroutine 1
        /Users/allen/go/pkg/mod/github.com/faiface/mainthread@v0.0.0-20171120011319-8b78f0a41ae3/mainthread.go:36 +0xbc

goroutine 1 [select, locked to thread]:
runtime.gopark(0x14000054ea8?, 0x2?, 0xa0?, 0x21?, 0x14000054e94?)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go:398 +0xc8 fp=0x14000054d40 sp=0x14000054d20 pc=0x104d07a28
runtime.selectgo(0x14000054ea8, 0x14000054e90, 0x0?, 0x0, 0x2?, 0x1)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/select.go:327 +0x608 fp=0x14000054e50 sp=0x14000054d40 pc=0x104d181f8
github.com/faiface/mainthread.Run(0x1400011e318)
        /Users/allen/go/pkg/mod/github.com/faiface/mainthread@v0.0.0-20171120011319-8b78f0a41ae3/mainthread.go:42 +0x110 fp=0x14000054ed0 sp=0x14000054e50 pc=0x104dfe0a0
github.com/AllenDang/giu.(*MasterWindow).Run(...)
        /Users/allen/Documents/Gocode/src/github.com/AllenDang/giu/MasterWindow.go:233
main.main()
        /Users/allen/Documents/Gocode/src/github.com/AllenDang/giu/examples/widgets/widgets.go:269 +0x170 fp=0x14000054f30 sp=0x14000054ed0 pc=0x104e10780
runtime.main()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go:267 +0x2bc fp=0x14000054fd0 sp=0x14000054f30 pc=0x104d075fc
runtime.goexit()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/asm_arm64.s:1197 +0x4 fp=0x14000054fd0 sp=0x14000054fd0 pc=0x104d36674

goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go:398 +0xc8 fp=0x14000044f90 sp=0x14000044f70 pc=0x104d07a28
runtime.goparkunlock(...)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go:404
runtime.forcegchelper()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go:322 +0xb8 fp=0x14000044fd0 sp=0x14000044f90 pc=0x104d078b8
runtime.goexit()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/asm_arm64.s:1197 +0x4 fp=0x14000044fd0 sp=0x14000044fd0 pc=0x104d36674
created by runtime.init.6 in goroutine 1
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go:310 +0x24

goroutine 18 [GC sweep wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go:398 +0xc8 fp=0x14000040760 sp=0x14000040740 pc=0x104d07a28
runtime.goparkunlock(...)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go:404
runtime.bgsweep(0x0?)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/mgcsweep.go:280 +0xa0 fp=0x140000407b0 sp=0x14000040760 pc=0x104cf3840
runtime.gcenable.func1()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/mgc.go:200 +0x28 fp=0x140000407d0 sp=0x140000407b0 pc=0x104ce85a8
runtime.goexit()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/asm_arm64.s:1197 +0x4 fp=0x140000407d0 sp=0x140000407d0 pc=0x104d36674
created by runtime.gcenable in goroutine 1
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/mgc.go:200 +0x6c

goroutine 19 [GC scavenge wait]:
runtime.gopark(0x1400008e000?, 0x105124ee0?, 0x1?, 0x0?, 0x140000824e0?)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go:398 +0xc8 fp=0x14000040f50 sp=0x14000040f30 pc=0x104d07a28
runtime.goparkunlock(...)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go:404
runtime.(*scavengerState).park(0x1052f8fa0)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/mgcscavenge.go:425 +0x5c fp=0x14000040f80 sp=0x14000040f50 pc=0x104cf10bc
runtime.bgscavenge(0x0?)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/mgcscavenge.go:653 +0x44 fp=0x14000040fb0 sp=0x14000040f80 pc=0x104cf1614
runtime.gcenable.func2()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/mgc.go:201 +0x28 fp=0x14000040fd0 sp=0x14000040fb0 pc=0x104ce8548
runtime.goexit()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/asm_arm64.s:1197 +0x4 fp=0x14000040fd0 sp=0x14000040fd0 pc=0x104d36674
created by runtime.gcenable in goroutine 1
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/mgc.go:201 +0xac

goroutine 34 [finalizer wait]:
runtime.gopark(0x8?, 0x10518a880?, 0x1?, 0x45?, 0x104d450e4?)
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go:398 +0xc8 fp=0x14000044580 sp=0x14000044560 pc=0x104d07a28
runtime.runfinq()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/mfinal.go:193 +0x108 fp=0x140000447d0 sp=0x14000044580 pc=0x104ce7698
runtime.goexit()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/asm_arm64.s:1197 +0x4 fp=0x140000447d0 sp=0x140000447d0 pc=0x104d36674
created by runtime.createfing in goroutine 1
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/mfinal.go:163 +0x80

r0      0x0
r1      0x0
r2      0x0
r3      0x0
r4      0x182360647
r5      0x16d151c80
r6      0x6e
r7      0x3
r8      0xb7c7c141955a3bf8
r9      0xb7c7c140f84f0bf8
r10     0x200
r11     0xb
r12     0xb
r13     0x1ff800
r14     0x7fb
r15     0x85635016
r16     0x148
r17     0x1e1f4b3a0
r18     0x0
r19     0x6
r20     0x16d153000
r21     0x1803
r22     0x16d1530e0
r23     0x600000008f00
r24     0xffffffffffffffff
r25     0x133828048
r26     0x1400011e318
r27     0x1
r28     0x140000031e0
r29     0x16d151bf0
lr      0x1823a3c28
sp      0x16d151bd0
pc      0x18236c764
fault   0x18236c764
exit status 2

Code example

main.go ```golang // put your code here! ```

To Reproduce

  1. Update master branch to lasted.
  2. cd to examples/widgets
  3. Invoke go run .
  4. will see the crash...

Version

master

OS

macOS v13.5.2

gucio321 commented 9 months ago

@AllenDang can't say whats wrong... It works for me. Could you try debugger?

voladelta commented 9 months ago

I have the same error as above

MacOS 13.6 Xcode Version 15.0 (15A240d) Go go1.21.1 darwin/arm64

i'm noob and from quick google, i found related issues

gucio321 commented 9 months ago

well, I see that there is something wrong with mainthread... Unfortunately I can't reproduce (as I have no access to any mac machine) and on my fedora linux everything works in both giu and cimgui-go.

@AllenDang could you take a look on this issue plz :pray:

voladelta commented 9 months ago

use https://pkg.go.dev/golang.design/x/hotkey/mainthread fix the issue for me

import ("golang.design/x/hotkey/mainthread") mainthread.Call(func() {

image

clean run without any issue. but i can't test on windows or linux 😅

from the readme in hotkey's repo

On macOS, due to the OS restriction (other platforms does not have this restriction), hotkey events must be handled on the "main thread". Therefore, in order to use this package properly, one must start an OS main event loop on the main thread, For self-contained applications, using golang.design/x/hotkey/mainthread is possible. It is uncessary or applications based on other GUI frameworks, such as fyne, ebiten, or Gio. See the "./examples" folder for more examples.

gucio321 commented 9 months ago

@voladelta thank you for your really valuale info! At the moment we use some mainthread library. I need to check its usage there...

jcbritobr commented 9 months ago

Will try to test here in fedora now.

jcbritobr commented 9 months ago

linux x64 fedora 39 - wayland go 1.21.1

For me its starts correctly, but in input I got a wrong key code hiting 5

2023/09/28 17:56:56 Unknown key: 325
**panic: Unknown key: 325**

goroutine 18 [running]:
log.Panicf({0x92d975?, 0xc000521c08?}, {0xc000521bf8?, 0x0?, 0x0?})
        /home/jgardona/Bin/go/src/log/log.go:439 +0x65
github.com/AllenDang/giu.keyFromGLFWKey(0x0?)
        /home/jgardona/Workspace/go/giu/Keycode.go:204 +0x105
github.com/AllenDang/giu.(*MasterWindow).SetInputHandler.func1(0x951f00?, 0xc000521cb8?, 0x557545?, 0x0)
        /home/jgardona/Workspace/go/giu/MasterWindow.go:381 +0x38
github.com/AllenDang/cimgui-go.keyCallback(0x0?, 0x145, 0x54, 0x1, 0x0)
        /home/jgardona/go/pkg/mod/github.com/!allen!dang/cimgui-go@v0.0.0-20230918114712-675654f053d9/backend.go:72 +0x67
github.com/AllenDang/cimgui-go._Cfunc_igRunLoop(0x1d009e0, 0x55ea60, 0x55eaa0, 0x55eae0, 0x55eb60)
        _cgo_gotypes.go:29014 +0x45
github.com/AllenDang/cimgui-go.(*GLFWBackend).Run.func1(0x1?)
        /home/jgardona/go/pkg/mod/github.com/!allen!dang/cimgui-go@v0.0.0-20230918114712-675654f053d9/glfw_backend.go:251 +0x9b
github.com/AllenDang/cimgui-go.(*GLFWBackend).Run(0x0?, 0x0?)
        /home/jgardona/go/pkg/mod/github.com/!allen!dang/cimgui-go@v0.0.0-20230918114712-675654f053d9/glfw_backend.go:251 +0x32
main.main.(*MasterWindow).Run.func1()
        /home/jgardona/Workspace/go/giu/MasterWindow.go:241 +0xcb
github.com/faiface/mainthread.Run.func1()
        /home/jgardona/go/pkg/mod/github.com/faiface/mainthread@v0.0.0-20171120011319-8b78f0a41ae3/mainthread.go:37 +0x23
created by github.com/faiface/mainthread.Run in goroutine 1
        /home/jgardona/go/pkg/mod/github.com/faiface/mainthread@v0.0.0-20171120011319-8b78f0a41ae3/mainthread.go:36 +0xae

Other way seems to be working on linux. Another issue I found is that some keys and multiline input doesn't works image

gucio321 commented 9 months ago

@AllenDang @voladelta could you try https://github.com/AllenDang/giu/pull/706 on macos

voladelta commented 9 months ago

@AllenDang @voladelta could you try #706 on macos

image

still crashed if you use Init, should replace to Call

gucio321 commented 9 months ago

@voladelta changeing this to Init breaks the example on linux... image

AllenDang commented 9 months ago

@gucio321 Maybe we could use a conditional build here to distinguish macOS/windows/linux

gucio321 commented 9 months ago

maybe, will do that later

gucio321 commented 8 months ago

@AllenDang @voladelta should work now. Sorry for delay but still don't have any computer dedicated for effective development

rokf commented 8 months ago

@gucio321 I've just tried it on macOS Sonoma (14.0) with the branch from your pull request and it seems to be working - the windows and widgets open and are responding. I've tried the helloworld and widgets examples.

I've noticed that the window closes with the following message in the console when I try to resize the window but that's probably not related to this issue:

File: /Users/runner/work/cimgui-go/cimgui-go/cimgui/imgui/imgui.cpp, Line: 6603

gucio321 commented 8 months ago

yes so its #716. This is fixed.