You do need to unref the menu if you don’t want it to leak upon destroying the window. There are no floating references here like with GtkWidget (GInitiallyUnowned).
What examples are you looking at? Many ‘official’ examples use GtkBuilder XML, where the menu is owned by the builder instance, e.g. the Application demo in gtk4-demo:
Yes, there’s no problem with two views sharing the same model.
I assume you understand that the GNOME project is not responsible for random examples appearing on Stack Overflow.
The answer you linked points to this tutorial; please, file an issue against it for clarification.
In general, the answer to the “who owns this data” is answered by the API reference: ownership can be transferred from the caller (the developer) to the callee (the function); it can be transferred from the callee to the caller; or it can be retained by the callee or the caller. For instance, the documentation of gtk_application_set_menubar() specifies that the GMenuModel representing the menu bar is owned by the caller of this function (typically, you); this means that you’re responsible for releasing the reference you have once you’re done with it.
I have not read the whole tutorial, so I can’t comment on it. I can only guarantee about the quality and maintenance of the tutorials on developer.gnome.org.
Now I’m slowly starting to understand how it works with the menu.
Every time I call gtk_box_append(GTK_BOX(vbox), CreateMenuButton(menu)); the reference counter of the menu object is increased, so at the end, as soon as all menus are loaded, I can call g_object_unref(menu); once.
As a test, after creating the menu, I did the following: g_object_set_data_full(G_OBJECT(menu),"test", menu, testProc); And in the testproc I looked at the ref_count, and it shows “0”. And testProc is called once when the program ends.
I have not read the whole tutorial, so I can’t comment on it. I can only guarantee about the quality and maintenance of the tutorials on developer.gnome.org .
I had a quick look and will take a closer look when I get the chance. I’m sure you’ll learn a lot.
As I have since found out, most things where GTK_xxx is written don’t need an unref, almost only for things with G_xxx.
Can you rely on the fact that if you query with ->ref_count before and after the commands and the counter is higher afterwards, you then have to do an unref yourself?
You can avoid releasing references on anything that inherits from GInitiallyUnowned (like GtkWidget) because the initial, “floating” reference is transferred (“sunk”) to another object, for instance when you add a child widget to a container. Some API in GTK is also annotated to use full ownership transfer when using parameters.
You have to understand how reference counting work, otherwise you should not be using the C API, and you’re much, much better off with using GTK in another programming languages.
No, you most definitely cannot access the ref_count field. The field is semi-public only for debugging purposes, but application code should never use it.