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

SetIcon() causes panic on Windows when called quickly after loading icon from disk #197

Closed
samschurter opened this issue Mar 15, 2021 · 2 comments · Fixed by #206
Closed

Comments

@samschurter
Copy link

I upgraded from github.com/getlantern/systray v0.0.0-20190727060347-6f0e5a3c556c to github.com/getlantern/systray v1.1.0 and ran into the following panic on Windows 10 (not tested on other OS):

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x1 addr=0x20 pc=0x18340b9]

goroutine 104 [running]:
github.com/getlantern/systray.(*winTray).setIcon(0x2538e60, 0xc0000871a0, 0x54, 0x0, 0x0)
        C:/Users/sschurte/go/workspace/pkg/mod/github.com/getlantern/systray@v1.1.0/systray_windows.go:220 +0xd9
github.com/getlantern/systray.SetIcon(0xc0002c8600, 0x12a6, 0x12a7)
        C:/Users/sschurte/go/workspace/pkg/mod/github.com/getlantern/systray@v1.1.0/systray_windows.go:839 +0x125
main.onReadyFunc.func1()
        C:/Users/sschurte/go/code/autoreporter/tray.go:27 +0x31d
github.com/getlantern/systray.Register.func2(0xc000281a40, 0xc000186250)
        C:/Users/sschurte/go/workspace/pkg/mod/github.com/getlantern/systray@v1.1.0/systray.go:94 +0x45
created by github.com/getlantern/systray.Register
        C:/Users/sschurte/go/workspace/pkg/mod/github.com/getlantern/systray@v1.1.0/systray.go:92 +0xe5

The code I was using to setup the icon is here:

func onReadyFunc(c *util.Config) func() {
	return func() {
		icon, err := getIcon("assets/images/test.ico")
		if err != nil {
			logger.Info(fmt.Sprintf("Failed to load tray icon: %v", err))
		} else {
			systray.SetIcon(icon)
		}
		// additional setup here, never reached
	}
}
//Helper to load a []byte to be used as a tray icon
func getIcon(s string) ([]byte, error) {
	b, err := os.ReadFile(s)
	if err != nil {
		return nil, err
	}
	return b, nil
}

I fixed the panic by adding a time.Sleep(100 * time.Millisecond) before the call to systray.SetIcon(icon). It seems to work reliably with any slight delay before the call. I discovered it by using fmt.Printf() to print the icon bytes to the console which takes time. A call to len(icon) didn't provide enough of a delay. Is there some race condition here?

@benjojo
Copy link

benjojo commented May 6, 2021

@samschurter Is it possible for you to write up a quick snippet to trigger this? I'm looking at merging #206 but I don't really have any test cases to verify that this the PR works

@samschurter
Copy link
Author

Unfortunately I can't reproduce this anymore, at least not without significant time to work on it. The app has moved forward and I've added things that are slowing down the startup like user input dialogs and database connections. My PC was also upgraded in the meantime and I think it has a faster SSD, so maybe that's affecting it as well.

When I just create a basic example program that does nothing but load the tray icon, it has no issues. Without time to begin with the app as it's working and slowly strip things out until I get back to a place where it panics, I can't reproduce this bug.

Since I have no failing example at all anymore, I will close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants