开发者

cannot passing structure over g_timeout_add?

开发者 https://www.devze.com 2023-02-08 14:59 出处:网络
ma dears, i got issues more here, which could\'t passing structure over g_timout_add with following code

ma dears, i got issues more here, which could't passing structure over g_timout_add with following code

$ cat tes.c 
#include <stdio.h>
#include <gtk/gtk.h>


typedef struct a
{
char *kaka;

}a;


gint zzz(a *ati)
{

printf("data after : %s\n",ati->kaka);

return 0;
}

int main(int argc,char* argv[])
{

gtk_init(&argc,&argv);
printf("starting...\n");

a *ati = g_slice_new(a);

ati->kaka = "rempong";

printf("data before : %s\n",ati->kaka);
g_timeout_add(2000,(GSourceFunc)zzz,ati);

g_slice_free(a,ati);

gtk_main();
return 0;
}

compiled :

开发者_如何学运维
gcc -o tes tes.c `pkg-config --libs --cflags gtk+-2.0`

running :

$ ./tes 
starting...
data before : rempong
data after : �W�
^C

anyone got any idea ?


You're freeing the data you're passing to the timeout function before you get a change to use it:

g_slice_free(a,ati);

Use destructors for that (see g_timeout_add_full()), or free it in the timeout handler (although doing that does not handle all cases).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号