macOS: crash when a native submenu is freed from an `id_pressed` handler (`NativeMenuMacOS::_menu_close_cb` dereferences a freed RID)
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 5Cause, as far as I can tell from the source:
- 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)withcall_deferred(native_menu_macos.mm, around line 127). - The item's action is queued in
deferred_menu_callsand runs later inDisplayServerMacOS::process_events()→PopupMenu::activate_item()→ the user'sid_pressedhandler. - The handler calls
root_menu.clear().PopupMenu::clear()callssubmenu->unbind_global_menu(), which callsNativeMenu::free_menu()on the submenu's RID immediately. - The queued
_menu_close_cb(rid)then runs with the freed RID.menus.get_or_null(p_rid)returnsnullptr, and the next line dereferences it without a check:
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 hereAbout 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:
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
- Run the MRP below on macOS.
- Click the tray (status bar) icon (a blue dot).
- Hover "Submenu 0" and click "Rebuild the menu".
- 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
; 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
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()Source: godotengine/godot