问题描述:

I have a gtk bin control and I want to remove it. That means I need it to be removed from operating memory as well as from window. I am removing it from window by simply calling window.Remove(child_item), but should I call Destroy() or Dispose() or both as well? In which order?

网友答案:

Dispose() is part of GObject, and it's used during the instance finalization sequence to release references and detach signal handlers; it can be called multiple times, in case of reference cycles, so it's a good practice to check for unset fields.

Destroy() is part of GtkWidget, and serves a similar purpose as the Dispose() implementation; Destroy() is pretty much an historical artefact of the fact that GObject started as an internal type of GTK before being moved out (alongside with the rest of the type system) into GLib. Destroy() is called as part of the default implementation of Dispose() inside GtkWidget.

if you want to remove a child from a container, you can simply call Remove(child) on the container, or Destroy() on the child: Remove() will release the reference held on the child, which will lead to the destruction of the widget, if that is the last reference. calling Destroy() on a GtkWidget will result on the widget being removed from its parent container. the two code paths should be interchangeable.

calling Dispose() directly is a Gtk#-ism: the Dispose() method is mostly intended to be overridden, not invoked directly.

相关阅读:
Top