GTK防止gtk_container_remove移除组件后被销毁

⌚Time: 2023-08-21 15:05:39

👨‍💻Author: Jack Ge

gtk_container_remove移除容器内组件


gtk_container_remove ()

void                gtk_container_remove                (GtkContainer *container,

                                                         GtkWidget *widget);

Removes widget from container. widget must be inside container. Note that container will own a reference to widget, and that this may be the last reference held; so removing a widget from its container can destroy that widget. If you want to use widget again, you need to add a reference to it while it's not inside a container, using g_object_ref(). If you don't want to use widget again it's usually more efficient to simply destroy it directly using gtk_widget_destroy() since this will remove it from the container and help break any circular reference count cycles.



container :



a GtkContainer

widget :



a current child of container

为了移除后还可以继续使用组件,使用g_object_ref增加对象的引用计数


g_object_ref ()

gpointer            g_object_ref                        (gpointer object);

Increases the reference count of object.

与之对应的是g_object_unref,减少对象引用计数,释放对象




g_object_unref ()

void                g_object_unref                      (gpointer object);

Decreases the reference count of object. When its reference count drops to 0, the object is finalized (i.e. its memory is freed).