#123936·godot

macOS: crash when a native submenu is freed from an `id_pressed` handler (`NativeMenuMacOS::_menu_close_cb` dereferences a freed RID)

Author: tsgx1990Created Sep 28, 2026Updated Sep 28, 2026
Labelsbugplatform:macostopic:platformsneeds workneeds testingcrash

Tested versions

  • Reproducible in: v4.7.1.stable.official [a13da4feb]
  • Not tested on master, but _menu_close_cb() is unchanged there as of 2026-09-29 (platform/macos/native_menu_macos.mm, last touched by 4c7aa7acb), so it is most likely reproducible too.

System information

macOS 26.4 (25E246) - Godot v4.7.1.stable - Compatibility renderer - Apple M1 Pro

Issue description

Clearing or rebuilding a native (global / status bar) menu from inside a submenu item's id_pressed handler crashes the engine:

ERROR: Attempting to use an uninitialized RID
   at: get_or_null (./core/templates/rid_owner.h:247)

================================================================
handle_crash: Program crashed with signal 5

Cause, as far as I can tell from the source:

  1. When the user clicks an item in a submenu, AppKit closes the menus and calls menuDidClose: → NativeMenuMacOS::_menu_close(), which queues _menu_close_cb(rid) with call_deferred (native_menu_macos.mm, around line 127).
  2. The item's action is queued in deferred_menu_calls and runs later in DisplayServerMacOS::process_events() → PopupMenu::activate_item() → the user's id_pressed handler.
  3. The handler calls root_menu.clear(). PopupMenu::clear() calls submenu->unbind_global_menu(), which calls NativeMenu::free_menu() on the submenu's RID immediately.
  4. The queued _menu_close_cb(rid) then runs with the freed RID. menus.get_or_null(p_rid) returns nullptr, and the next line dereferences it without a check:
cpp
void NativeMenuMacOS::_menu_close_cb(const RID &p_rid) {
	MenuData *md = menus.get_or_null(p_rid);
	if (md->close_cb.is_valid()) { // md is nullptr here

About the misleading error text: for a freed slot (validator 0xFFFFFFFF), get_or_null() is supposed to return nullptr silently; the "uninitialized RID" message is only for allocated-but-uninitialized slots. But because _menu_close_cb() dereferences the result unconditionally, the compiler treats the nullptr paths as unreachable and merges them: in the official 4.7.1 macOS binary, the function that references the strings "_menu_close_cb" and "Failed to execute menu close callback: %s." branches on a validator mismatch straight into a block that prints Attempting to use an uninitialized RID (rid_owner.h:247) and then executes brk #1. The crash report's faulting address is that brk. So any stale RID here shows up as "uninitialized RID" + signal 5.

_menu_open() and _menu_need_update() already check md for null; _menu_close_cb() doesn't. A possible fix:

cpp
void NativeMenuMacOS::_menu_close_cb(const RID &p_rid) {
	MenuData *md = menus.get_or_null(p_rid);
	if (!md) {
		return; // The menu was freed after its close notification was queued.
	}
	...

Rebuilding a menu when an item is clicked is a common pattern (for example, a tray menu that lists items the user can add or remove), and it is legal on the GDScript side, so I don't think user code should be required to avoid it. Workaround until fixed: defer the rebuild (_rebuild.call_deferred()), so the already-queued close callback runs first while the RID is still valid.

Steps to reproduce

  1. Run the MRP below on macOS.
  2. Click the tray (status bar) icon (a blue dot).
  3. Hover "Submenu 0" and click "Rebuild the menu".
  4. The engine crashes with the error above.

Changing _rebuild() to _rebuild.call_deferred() in _on_sub_pressed should avoid the crash, since the already-queued close callback then runs before the RID is freed (that is the workaround we use in our project).

Minimal reproduction project (MRP)

The MRP is three small text files (a StatusIndicator with a native PopupMenu and one submenu; clicking the submenu item calls root_menu.clear() and rebuilds the menu). The tray icon is drawn in code, so no imported resources are needed. Save them into an empty folder and run it with Godot --path <folder>.

project.godot

ini
; Engine configuration file.
config_version=5

[application]

config/name="MRP macOS native menu close callback crash"
run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.7", "GL Compatibility")

[rendering]

renderer/rendering_method="gl_compatibility"
renderer/rendering_method.mobile="gl_compatibility"

main.tscn

[gd_scene load_steps=2 format=3]

[ext_resource type="Script" path="res://main.gd" id="1"]

[node name="Main" type="Node"]
script = ExtResource("1")

main.gd

gdscript
extends Node
# MRP: rebuilding a native (macOS) menu from a submenu item's `id_pressed` handler crashes the engine.
#
# Click the tray (status bar) icon -> hover "Submenu 0" -> click "Rebuild the menu".
# `root_menu.clear()` unbinds the submenu and frees its native menu RID right away, but the submenu's
# close callback (`NativeMenuMacOS::_menu_close_cb`) was already queued with `call_deferred` when the
# menu closed. It runs afterwards with the freed RID and dereferences the null `MenuData`:
#   ERROR: Attempting to use an uninitialized RID
#   handle_crash: Program crashed with signal 5
#
# Likely workaround: defer the rebuild (`_rebuild.call_deferred()`), so the queued close callback runs first.

var root_menu: PopupMenu
var old_sub: PopupMenu
var count := 0

func _ready() -> void:
	var tray := StatusIndicator.new()
	tray.icon = _make_icon()
	tray.tooltip = "MRP"
	add_child(tray)
	root_menu = PopupMenu.new()
	root_menu.prefer_native_menu = true
	tray.add_child(root_menu)
	tray.menu = tray.get_path_to(root_menu)
	_rebuild()

# A blue dot drawn in code, so the MRP needs no imported resources.
func _make_icon() -> Texture2D:
	var img := Image.create_empty(32, 32, false, Image.FORMAT_RGBA8)
	for y in 32:
		for x in 32:
			if Vector2(x - 15.5, y - 15.5).length() <= 12.0:
				img.set_pixel(x, y, Color("#478cbf"))
	return ImageTexture.create_from_image(img)

func _rebuild() -> void:
	root_menu.clear()
	if old_sub:
		old_sub.queue_free()
	var sub := PopupMenu.new()
	sub.prefer_native_menu = true
	sub.add_item("Rebuild the menu", 1)
	sub.id_pressed.connect(_on_sub_pressed)
	root_menu.add_submenu_node_item("Submenu %d" % count, sub)
	root_menu.add_item("Quit", 2)
	if not root_menu.id_pressed.is_connected(_on_root_pressed):
		root_menu.id_pressed.connect(_on_root_pressed)
	old_sub = sub

func _on_sub_pressed(_id: int) -> void:
	count += 1
	print("rebuilding the menu from inside the submenu's id_pressed (", count, ")")
	_rebuild()  # crashes; deferring it (`_rebuild.call_deferred()`) should not

func _on_root_pressed(id: int) -> void:
	if id == 2:
		get_tree().quit()