fyne-io/fyne · error

toNativePtr() failed "%s": %s

Error message

toNativePtr() failed "%s": %s

What it means

toNativePtr converts Go strings to NUL-terminated UTF-16 for Windows APIs - specifically MessageBoxW, which the glfw driver uses to surface init failures on Windows. syscall.UTF16PtrFromString fails exactly when the string contains an embedded NUL (U+0000), because NUL is the terminator; the helper panics, masking the original init error it was about to display.

Source

Thrown at internal/driver/glfw/driver_windows.go:36

	MB_ICONERROR MB = 0x0000_0010

	ES_CONTINUOUS       ES = 0x80000000
	ES_DISPLAY_REQUIRED ES = 0x00000002
)

var (
	kernel32 = syscall.NewLazyDLL("kernel32.dll")
	user32   = syscall.NewLazyDLL("user32.dll")

	executionState     = kernel32.NewProc("SetThreadExecutionState")
	MessageBox         = user32.NewProc("MessageBoxW")
	getDoubleClickTime = user32.NewProc("GetDoubleClickTime")
)

func toNativePtr(s string) *uint16 {
	pstr, err := syscall.UTF16PtrFromString(s)
	if err != nil {
		panic(fmt.Sprintf("toNativePtr() failed \"%s\": %s", s, err))
	}
	return pstr
}

// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxw
func messageBoxError(text, caption string) {
	uType := MB_OK | MB_ICONERROR

	syscall.SyscallN(MessageBox.Addr(),
		uintptr(unsafe.Pointer(nil)), uintptr(unsafe.Pointer(toNativePtr(text))),
		uintptr(unsafe.Pointer(toNativePtr(caption))), uintptr(uType))
}

func logError(msg string, err error) {
	text := fmt.Sprintf("Fyne error: %v", msg)
	if err != nil {
		text = text + fmt.Sprintf("\n  Cause:%v", err)
	}

View on GitHub (pinned to 8860ee95c3)

Solutions

  1. Sanitize text before it can reach error display: strip NUL bytes (strings.ReplaceAll(s, "\x00", ""))
  2. Fix the origin producing NULs in the message (validate binary input, trim buffers)
  3. On Windows, read the stderr/fyne log to see the underlying init error instead of relying on the message box

Example fix

// before
msg := fmt.Sprintf("window creation failed: %s", string(rawBytes)) // may contain NUL

// after
msg := "window creation failed: " + strings.ReplaceAll(string(rawBytes), "\x00", "")
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight: make strings safe for Windows wide-char APIs before display.
func displaySafe(s string) string { return strings.ReplaceAll(s, "\x00", "") }

if strings.IndexByte(msg, 0) >= 0 { msg = displaySafe(msg) }

Prevention

When it happens

Trigger: A Windows driver-init failure path reaching messageBoxError where the error message or caption contains a literal NUL byte - errors built from binary data, truncated reads, or control characters in log text.

Common situations: Error strings concatenated from raw bytes (file/network parsing); binary formats leaking NULs into messages; rare overall, but when hit the dialog never appears and the panic hides the real cause.

Related errors


AI-assisted analysis of fyne-io/fyne@8860ee95c3 (2026-08-15). Data as JSON: /api/errors/4db8beb796a949b7. Report an issue: GitHub.