[GH-ISSUE #425] Panic on mouse move #312

Closed
opened 2026-03-04 01:03:51 +03:00 by kerem · 1 comment
Owner

Originally created by @k33nice on GitHub (Apr 3, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/425

I've updated the module to the recent version with mouse support and I get panic in my app with the code similar to below.

package main

import (
	"github.com/gdamore/tcell"
	"github.com/rivo/tview"
)

func main() {
	app := tview.NewApplication()
	app.EnableMouse(true)

	main := tview.NewBox()
	pages := tview.NewPages()
	pages.AddPage("main", main, true, true)

	app.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
		switch e.Key() {
		case tcell.KeyTab:
			popup(app, pages, "test", "test modal")
		}

		return e
	})

	if err := app.SetRoot(pages, true).Run(); err != nil {
		panic(err)
	}
}

func popup(app *tview.Application, pages *tview.Pages, title, content string) {
	_, _, width, _ := pages.GetRect()
	innerWidth := int(float32(width) * 0.3)
	innerHeight := len(tview.WordWrap(content, innerWidth))

	text := tview.NewTextView()
	text.SetText(content).
		SetBorder(true).SetBorderPadding(0, 0, 1, 1)

	pages.AddPage("modal", modal(text, innerWidth+2, innerHeight+2), true, true)
	app.SetFocus(text)
}

func modal(primitive tview.Primitive, width, height int) tview.Primitive {
	return tview.NewFlex().
		AddItem(nil, 0, 1, false).
		AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
			AddItem(nil, 0, 1, false).
			AddItem(primitive, height, 1, false).
			AddItem(nil, 0, 1, false), width, 1, false).
		AddItem(nil, 0, 1, false)
}

When the popup is shown (press TAB key if you run the code above), moving the mouse causes panic.

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x1144ab2]

goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1(0xc000188000)
	/Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/application.go:245 +0x82
panic(0x116c400, 0x12bd7f0)
	/usr/local/Cellar/go/1.14.1/libexec/src/runtime/panic.go:967 +0x166
github.com/rivo/tview.(*Flex).MouseHandler.func1(0x1170000, 0xc00020e180, 0xc0004ca160, 0x113cbb3, 0xc0000b81a0, 0xc0004ca170)
	/Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/flex.go:214 +0xc2
github.com/rivo/tview.(*Box).WrapMouseHandler.func1(0xc000090000, 0xc00020e180, 0xc0004ca160, 0x100c408, 0xc000080400, 0x1335e98)
	/Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/box.go:212 +0x7f
github.com/rivo/tview.(*Pages).MouseHandler.func1(0x1170000, 0xc00020e180, 0xc0004ca160, 0x10, 0x1170bc0, 0xc0004ca101)
	/Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/pages.go:293 +0xe9
github.com/rivo/tview.(*Box).WrapMouseHandler.func1(0x1170000, 0xc00020e180, 0xc0004ca160, 0xc000187c30, 0xc000187c30, 0x10419fc)
	/Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/box.go:212 +0x7f
github.com/rivo/tview.(*Application).fireMouseActions.func1(0x0)
	/Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/application.go:419 +0x155
github.com/rivo/tview.(*Application).fireMouseActions(0xc000188000, 0xc00020e180, 0x0)
	/Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/application.go:436 +0x48b
github.com/rivo/tview.(*Application).Run(0xc000188000, 0x0, 0x0)
	/Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/application.go:360 +0x296
main.main()
	/Users/k33nice/dev/golang/tview/main.go:25 +0x2e3
exit status 2
Originally created by @k33nice on GitHub (Apr 3, 2020). Original GitHub issue: https://github.com/rivo/tview/issues/425 I've updated the module to the recent version with mouse support and I get panic in my app with the code similar to below. ```go package main import ( "github.com/gdamore/tcell" "github.com/rivo/tview" ) func main() { app := tview.NewApplication() app.EnableMouse(true) main := tview.NewBox() pages := tview.NewPages() pages.AddPage("main", main, true, true) app.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey { switch e.Key() { case tcell.KeyTab: popup(app, pages, "test", "test modal") } return e }) if err := app.SetRoot(pages, true).Run(); err != nil { panic(err) } } func popup(app *tview.Application, pages *tview.Pages, title, content string) { _, _, width, _ := pages.GetRect() innerWidth := int(float32(width) * 0.3) innerHeight := len(tview.WordWrap(content, innerWidth)) text := tview.NewTextView() text.SetText(content). SetBorder(true).SetBorderPadding(0, 0, 1, 1) pages.AddPage("modal", modal(text, innerWidth+2, innerHeight+2), true, true) app.SetFocus(text) } func modal(primitive tview.Primitive, width, height int) tview.Primitive { return tview.NewFlex(). AddItem(nil, 0, 1, false). AddItem(tview.NewFlex().SetDirection(tview.FlexRow). AddItem(nil, 0, 1, false). AddItem(primitive, height, 1, false). AddItem(nil, 0, 1, false), width, 1, false). AddItem(nil, 0, 1, false) } ``` When the popup is shown (press TAB key if you run the code above), moving the mouse causes panic. ``` panic: runtime error: invalid memory address or nil pointer dereference [recovered] panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x1144ab2] goroutine 1 [running]: github.com/rivo/tview.(*Application).Run.func1(0xc000188000) /Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/application.go:245 +0x82 panic(0x116c400, 0x12bd7f0) /usr/local/Cellar/go/1.14.1/libexec/src/runtime/panic.go:967 +0x166 github.com/rivo/tview.(*Flex).MouseHandler.func1(0x1170000, 0xc00020e180, 0xc0004ca160, 0x113cbb3, 0xc0000b81a0, 0xc0004ca170) /Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/flex.go:214 +0xc2 github.com/rivo/tview.(*Box).WrapMouseHandler.func1(0xc000090000, 0xc00020e180, 0xc0004ca160, 0x100c408, 0xc000080400, 0x1335e98) /Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/box.go:212 +0x7f github.com/rivo/tview.(*Pages).MouseHandler.func1(0x1170000, 0xc00020e180, 0xc0004ca160, 0x10, 0x1170bc0, 0xc0004ca101) /Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/pages.go:293 +0xe9 github.com/rivo/tview.(*Box).WrapMouseHandler.func1(0x1170000, 0xc00020e180, 0xc0004ca160, 0xc000187c30, 0xc000187c30, 0x10419fc) /Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/box.go:212 +0x7f github.com/rivo/tview.(*Application).fireMouseActions.func1(0x0) /Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/application.go:419 +0x155 github.com/rivo/tview.(*Application).fireMouseActions(0xc000188000, 0xc00020e180, 0x0) /Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/application.go:436 +0x48b github.com/rivo/tview.(*Application).Run(0xc000188000, 0x0, 0x0) /Users/k33nice/dev/golang/tview/vendor/github.com/rivo/tview/application.go:360 +0x296 main.main() /Users/k33nice/dev/golang/tview/main.go:25 +0x2e3 exit status 2 ```
kerem closed this issue 2026-03-04 01:03:51 +03:00
Author
Owner

@rivo commented on GitHub (Apr 4, 2020):

The latest commit fixes this. Thanks for letting me know.

<!-- gh-comment-id:609083817 --> @rivo commented on GitHub (Apr 4, 2020): The latest commit fixes this. Thanks for letting me know.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#312
No description provided.