- 论坛徽章:
- 0
|
初学者一个,在gtk。org看到了关于GtkObject的一段描述:
GtkObject is the base class for all widgets, and for a few non-widget objects such as GtkAdjustment. GtkObject predates GObject; non-widgets that derive from GtkObject rather than GObject do so for backward compatibility reasons.
GtkObjects are created with a "floating" reference count. This means that the initial reference is not owned by anyone. Calling g_object_unref() on a newly-created GtkObject is incorrect, the floating reference has to be removed first. This can be done by anyone at any time, by calling g_object_ref_sink() to convert the floating reference into a regular reference. g_object_ref_sink() returns a new reference if an object is already sunk (has no floating reference).
When you add a widget to its parent container, the parent container will do this:
g_object_ref_sink (G_OBJECT (child_widget));
This means that the container now owns a reference to the child widget and the child widget has no floating reference.
The purpose of the floating reference is to keep the child widget alive until you add it to a parent container:
button = gtk_button_new ();
/* button has one floating reference to keep it alive */
gtk_container_add (GTK_CONTAINER (container), button);
/* button has one non-floating reference owned by the container */
GtkWindow is a special case, because GTK+ itself will ref/sink it on creation. That is, after calling gtk_window_new(), the GtkWindow will have one reference which is owned by GTK+, and no floating references.
One more factor comes into play: the "destroy" signal, emitted by the gtk_object_destroy() method. The "destroy" signal asks all code owning a reference to an object to release said reference. So, for example, if you call gtk_object_destroy() on a GtkWindow, GTK+ will release the reference count that it owns; if you call gtk_object_destroy() on a GtkButton, then the button will be removed from its parent container and the parent container will release its reference to the button. Because these references are released, calling gtk_object_destroy() should result in freeing all memory associated with an object, unless some buggy code fails to release its references in response to the "destroy" signal. Freeing memory (referred to as finalization only happens if the reference count reaches zero.
Some simple rules for handling ""
*
Never call g_object_unref() unless you have previously called g_object_ref(), even if you created the GtkObject. (Note: this is not true for GObject; for GObject, the creator of the object owns a reference.)
*
Call gtk_object_destroy() to get rid of most objects in most cases. In particular, widgets are almost always destroyed in this way.
*
Because of the floating reference count, you don't need to worry about reference counting for widgets and toplevel windows, unless you explicitly call g_object_ref() yourself.
1.对于GtkObject派生的子类,上面说我们不应该调用g_object_unref()除非之前用过g_object_ref,有没有例外呢?
2.对于从GObject派生的子类,如GtkAction, GtkUIManager,因为他们创建后不是floating的ref,是不是我们都应该用g_object_unref去释放对象的内存呢?
3.g_object_unref应该在什么时候正确的使用?
4.如果我对GObject派生的子类,不调用g_object_unref会不会导致内存泄漏?还是Gtk系统在你程序退出后都会做一次统一的清理?
初学者,对Gtk对象的释放使用比较疑惑,官网上并没有描述那些对象在create后需要你手动的去释放。还请老大们帮忙解答一下。
THX. |
|