g_object_set_data 有记忆漏洞
g_object_set_data has memory holes
我正在制作一个 GTK3 应用程序,我在 GtkListBoxRow 上使用 g_object_set_qdata
进行搜索。
我使用这个代码:
GArray *complib;
#define row_get_entry(row) (ElcCompLibEntry*)g_object_get_qdata(G_OBJECT(row),registryquark)
GtkWidget* elc_registry_listbox_make_item(ElcCompLibEntry* curritem)
{
/* I remove UI creation */
GtkWidget* intermediate = gtk_list_box_row_new();
gtk_container_add(GTK_CONTAINER(intermediate),GTK_WIDGET(mainbox));
gtk_widget_show_all(GTK_WIDGET(intermediate));
g_object_set_qdata(G_OBJECT(intermediate),registryquark,curritem);
return intermediate;
};
static GtkWidget* saved = NULL;
GtkWidget* new=elc_registry_listbox_make_item(entry);
gtk_container_add(GTK_CONTAINER(listbox->real_listbox),new);
if (saved)
g_debug("saved entry is at position %d",((gintptr)row_get_entry(saved)-(gintptr)complib->data)/sizeof(ElcCompLibEntry));
saved=new;
g_debug("entry is at position %d",((gintptr)row_get_entry(new)-(gintptr)complib->data)/sizeof(ElcCompLibEntry));
我程序的输出:
** (elc:9906): DEBUG: 18:51:10.623: entry is at position 0
** (elc:9906): DEBUG: 18:51:10.625: saved entry is at position -14015
** (elc:9906): DEBUG: 18:51:10.625: entry is at position 1
** (elc:9906): DEBUG: 18:51:10.627: saved entry is at position 32
** (elc:9906): DEBUG: 18:51:10.627: entry is at position 2
** (elc:9906): DEBUG: 18:51:10.629: saved entry is at position 2
** (elc:9906): DEBUG: 18:51:10.629: entry is at position 3
** (elc:9906): DEBUG: 18:51:10.631: saved entry is at position 25921
** (elc:9906): DEBUG: 18:51:10.631: entry is at position 4
ElcCompLibEntry
是一个简单的结构。
g_object_set_qdata_full
只是破坏旧数据,夸克被初始化,即使 运行 它在主线程中我得到相同的结果(只有 saved entry is at position 2
是常量)。
谁能帮我找到问题的根源and/or 发给我相关文档?
感谢您的宝贵时间!
根据 https://developer.gnome.org/glib/stable/glib-Arrays.html#GArray、
The data may be moved as elements are added to the GArray.
如果没有看到创建和追加到 complib
的代码,很难说清楚,但是 complib->data
不是常量,当调整数组大小时,存储在 qdata 中的指针可能会失效。
为防止出现这种情况,请尝试使用g_array_sized_new()
预先分配您需要的元素数量来创建数组。
我正在制作一个 GTK3 应用程序,我在 GtkListBoxRow 上使用 g_object_set_qdata
进行搜索。
我使用这个代码:
GArray *complib;
#define row_get_entry(row) (ElcCompLibEntry*)g_object_get_qdata(G_OBJECT(row),registryquark)
GtkWidget* elc_registry_listbox_make_item(ElcCompLibEntry* curritem)
{
/* I remove UI creation */
GtkWidget* intermediate = gtk_list_box_row_new();
gtk_container_add(GTK_CONTAINER(intermediate),GTK_WIDGET(mainbox));
gtk_widget_show_all(GTK_WIDGET(intermediate));
g_object_set_qdata(G_OBJECT(intermediate),registryquark,curritem);
return intermediate;
};
static GtkWidget* saved = NULL;
GtkWidget* new=elc_registry_listbox_make_item(entry);
gtk_container_add(GTK_CONTAINER(listbox->real_listbox),new);
if (saved)
g_debug("saved entry is at position %d",((gintptr)row_get_entry(saved)-(gintptr)complib->data)/sizeof(ElcCompLibEntry));
saved=new;
g_debug("entry is at position %d",((gintptr)row_get_entry(new)-(gintptr)complib->data)/sizeof(ElcCompLibEntry));
我程序的输出:
** (elc:9906): DEBUG: 18:51:10.623: entry is at position 0
** (elc:9906): DEBUG: 18:51:10.625: saved entry is at position -14015
** (elc:9906): DEBUG: 18:51:10.625: entry is at position 1
** (elc:9906): DEBUG: 18:51:10.627: saved entry is at position 32
** (elc:9906): DEBUG: 18:51:10.627: entry is at position 2
** (elc:9906): DEBUG: 18:51:10.629: saved entry is at position 2
** (elc:9906): DEBUG: 18:51:10.629: entry is at position 3
** (elc:9906): DEBUG: 18:51:10.631: saved entry is at position 25921
** (elc:9906): DEBUG: 18:51:10.631: entry is at position 4
ElcCompLibEntry
是一个简单的结构。
g_object_set_qdata_full
只是破坏旧数据,夸克被初始化,即使 运行 它在主线程中我得到相同的结果(只有 saved entry is at position 2
是常量)。
谁能帮我找到问题的根源and/or 发给我相关文档?
感谢您的宝贵时间!
根据 https://developer.gnome.org/glib/stable/glib-Arrays.html#GArray、
The data may be moved as elements are added to the GArray.
如果没有看到创建和追加到 complib
的代码,很难说清楚,但是 complib->data
不是常量,当调整数组大小时,存储在 qdata 中的指针可能会失效。
为防止出现这种情况,请尝试使用g_array_sized_new()
预先分配您需要的元素数量来创建数组。