g3n / engine

Go 3D Game Engine (http://g3n.rocks)
https://discord.gg/NfaeVr8zDg
BSD 2-Clause "Simplified" License
2.74k stars 292 forks source link

[BUG] Program crash after left/right click before move the cursor #299

Closed zyxkad closed 9 months ago

zyxkad commented 9 months ago

Description

After scrolling and drag camera with secondary button, program will randomly crashed. Used the README's demo code

Logs

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x104af6a00]

goroutine 1 [running, locked to thread]:
github.com/g3n/engine/gui.(*manager).onCursor.func1({0x104bfaaf0, 0x1400028c000})
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/gui/manager.go:190 +0x30
github.com/g3n/engine/gui.traverseIPanel({0x104bfaaf0, 0x1400028c000}, 0x14000153938)
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/gui/manager.go:254 +0x5c
github.com/g3n/engine/gui.traverseINode({0x104bf9e30, 0x1400028c000}, 0x14000126140?)
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/gui/manager.go:267 +0x68
github.com/g3n/engine/gui.traverseINode({0x104bf9bc0, 0x14000126140}, 0x14000153918?)
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/gui/manager.go:270 +0x90
github.com/g3n/engine/gui.(*manager).forEachIPanel(...)
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/gui/manager.go:278
github.com/g3n/engine/gui.(*manager).onCursor(0x1400015c240, {0x104b3ca2c, 0xa}, {0x104bada00?, 0x0})
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/gui/manager.go:189 +0xd4
github.com/g3n/engine/gui.(*manager).SetCursorFocus(...)
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/gui/manager.go:93
github.com/g3n/engine/camera.(*OrbitControl).onMouse(0x1400014c480, {0x104b3ce68?, 0x14000153a01?}, {0x104bad940?, 0x1400015a070?})
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/camera/orbit_control.go:241 +0xd8
github.com/g3n/engine/core.(*Dispatcher).Dispatch(0x104bf9bc0?, {0x104b3ce68, 0xb}, {0x104bad940, 0x1400015a070})
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/core/dispatcher.go:108 +0x98
github.com/g3n/engine/gui.(*manager).onMouse(0x1400015c240, {0x104b3ce68, 0xb}, {0x104bad940, 0x1400015a070})
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/gui/manager.go:141 +0x12c
github.com/g3n/engine/core.(*Dispatcher).Dispatch(0x14000153bd8?, {0x104b3ce68, 0xb}, {0x104bad940, 0x1400015a070})
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/core/dispatcher.go:108 +0x98
github.com/g3n/engine/window.Init.func3(0x104e82d20?, 0x1, 0x0, 0x0)
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/window/glfw.go:329 +0x84
github.com/go-gl/glfw/v3.3/glfw.goMouseButtonCB(0x0?, 0x1, 0x0, 0x0)
        /Users/ckpn/Mine/projects/golang/pkg/mod/github.com/go-gl/glfw/v3.3/glfw@v0.0.0-20210410170116-ea3d685f79fb/input.go:334 +0x60
github.com/go-gl/glfw/v3.3/glfw._Cfunc_glfwPollEvents()
        _cgo_gotypes.go:1497 +0x30
github.com/go-gl/glfw/v3.3/glfw.PollEvents()
        /Users/ckpn/Mine/projects/golang/pkg/mod/github.com/go-gl/glfw/v3.3/glfw@v0.0.0-20210410170116-ea3d685f79fb/window.go:949 +0x1c
github.com/g3n/engine/window.(*GlfwWindow).PollEvents(...)
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/window/glfw.go:441
github.com/g3n/engine/app.(*Application).Run(0x14000072060, 0x14000153f18)
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/test/g3n/app/app-desktop.go:85 +0x88
main.main()
        /Users/ckpn/Mine/projects/golang/src/github.com/LiterMC/curve/main.go:35 +0x94
exit status 2
zyxkad commented 9 months ago

Found the stable way to reproduce it:

  1. move your cursor, ensure it will right on the window after the program started
  2. go run ***, do not move your cursor after the window appread.
  3. hold right click, then drag the mouse, after released it, program will crash
zyxkad commented 9 months ago

The issue is (*manager)(gm).cev is nil before cursor starts moving

danaugrs commented 9 months ago

Hi! This seems to happen only on macOS and is likely due to GLTF behaving differently there. It looks like on macOS mouse button callbacks can be called without cursor position callbacks first being called, leading to cev being nil and causing that crash. There's a workaround here that is similar to your PR. Thanks for finding this bug and fixing it!