GtkMenuButton Menu Model causes crashes in Windows-compiled app

Your Windows environment uses Gtk 4.16 but support for inline menu definitions like in your UI example was not added before Gtk 4.17. See Gtk news:

Do read these news files; they provide valuable information.

And do handle errors when loading UI files. Your C example doesn’t, and hence fails silently. Had you passed an error pointer to the gtk_builder_add_from_file call you might have seen a similar error as in your Rust code.

Your Rust code crashes because you’re calling gtk::Builder::from_string, i.e. gtk_builder_new_from_string which explicitly crashes when given an invalid UI definition.

For backwards compatibility define the menu separately, and then reference it by name in the menu-model property definition. Roughly like this (untested):

<?xml version='1.0' encoding='UTF-8'?>
<interface>
  <requires lib="gio" version="2.0"/>
  <requires lib="gtk" version="4.12"/>
  <object class="GtkMenuButton" id="button">
    <property name="menu-model">test-menu</property>
  </object>
  <menu id="test-menu">
    <item>
      <attribute name="action">window.close</attribute>
      <attribute name="label">Close Window</attribute>
    </item>
  </menu>
</interface>
2 Likes