Fix GTK-CRITICAL when SetBitmap() is called after Append() on a wxMenuItem (PR #26771)

17 views
Skip to first unread message

ykne

unread,
Aug 1, 2026, 8:58:42 PM (3 days ago) Aug 1
to wx-...@googlegroups.com, Subscribed

Bug

wxMenu::GtkAppend() (src/gtk/menu.cpp) decides whether to build a
GtkImageMenuItem or a plain GtkMenuItem based on
mitem->GetBitmap().IsOk() at Append() time:

case wxITEM_NORMAL:
    if (mitem->GetBitmap().IsOk())
        menuItem = gtk_image_menu_item_new_with_label("");
    else
    {
        const char* stockid = wxGetStockGtkID(mitem->GetId());
        if (stockid)
            menuItem = gtk_image_menu_item_new_from_stock(stockid, nullptr);
        else
            menuItem = gtk_menu_item_new_with_label(""); // <-- plain item
    }

But wxMenu::Append() returns the new wxMenuItem* precisely to
support the common idiom:

menu->Append(id, label)->SetBitmap(bmp);

Here SetBitmap() runs after Append() already built the
underlying GTK widget. For any non-stock id, that widget ends up
being a plain GtkMenuItem. Later, whenever the menu is actually
shown (wxWindowGTK::DoPopupMenu()wxMenu::SetupBitmaps()
wxMenuItem::SetupBitmaps()), wx calls:

gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(m_menuItem), image);

on that plain GtkMenuItem, which trips the GTK_IS_IMAGE_MENU_ITEM
assertion:

Gtk-CRITICAL **: gtk_image_menu_item_set_image: assertion 'GTK_IS_IMAGE_MENU_ITEM (image_menu_item)' failed

and the bitmap is silently never attached.

Repro

Minimal standalone repro (not included in this PR, happy to attach if
useful):

wxMenu menu;
menu.Append(wxID_HIGHEST + 1000, "Test Item")->SetBitmap(wxBitmap(16, 16));
PopupMenu(&menu, wxPoint(20, 20));

Confirmed present on both the 3.2 branch and master, built from
source (not just the Fedora-packaged wx), with --with-gtk=3 and
system third-party libs.

Fix

Always build a GtkImageMenuItem for wxITEM_NORMAL (unless a stock
GTK id already provides one via gtk_image_menu_item_new_from_stock).
An empty GtkImageMenuItem behaves identically to a plain
GtkMenuItem, so this is a no-op for items that never get a bitmap,
and fixes the assertion (and the missing icon) for items that get
SetBitmap() called after Append().

Verified: rebuilt both 3.2 and master from source with the fix
applied, reran the repro above — no assertion, clean stderr.

A separate PR with the same fix against the 3.2 branch will follow
if useful; let me know your preference on which branch should take
this first.

🤖 Generated with Claude Code


You can view, comment on, or merge this pull request online at:

  https://github.com/wxWidgets/wxWidgets/pull/26771

Commit Summary

  • 5c1c3e4 Always create a GtkImageMenuItem for wxITEM_NORMAL items in wxGTK

File Changes

(1 file)

Patch Links:


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/26771@github.com>

VZ

unread,
Aug 2, 2026, 1:23:18 PM (2 days ago) Aug 2
to wx-...@googlegroups.com, Subscribed
vadz left a comment (wxWidgets/wxWidgets#26771)

Thanks, I agree that using SetBitmap() should work, of course, but OTOH it seems a bit wrong to unconditionally create image menu items when many (most?) of them are not going to use images. I think replacing the existing item with an image item in SetBitmap(), if necessary, should work too, but I'm not sure if it's worth it.

I believe that the really correct thing to do would be stop using GtkImageMenuItem entirely and always create a box ourselves and then pack whatever we need into it, just as the documentation for this class suggests. But this would require many more changes...

@paulcor Do you have any thoughts about this?


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/26771/c5159495506@github.com>

paulcor

unread,
Aug 2, 2026, 1:43:48 PM (2 days ago) Aug 2
to wx-...@googlegroups.com, Subscribed
paulcor left a comment (wxWidgets/wxWidgets#26771)

This is fine.


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/26771/c5159572649@github.com>

paulcor

unread,
Aug 2, 2026, 1:43:57 PM (2 days ago) Aug 2
to wx-...@googlegroups.com, Subscribed

Merged #26771 into master.


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/26771/issue_event/28854490028@github.com>

Reply all
Reply to author
Forward
0 new messages