Re: passing args
- From: "DINH V. Hoà" <dinh enserb fr>
- To: Juergen Boemmels <boemmels physik uni-kl de>, gtk-list <gtk-list gnome org>
- Subject: Re: passing args
- Date: Thu, 16 Nov 2000 00:25:15 +0000
Juergen Boemmels wrote:
>
> "DINH V. Hoà" <dinh enserb fr> writes:
>
> > to call the function, you do the following :
> >
> > {
> > struct struct_arg1_arg2 data;
> > data->arg1 = arg1;
> > data->arg2 = arg2;
> > gtk_timeout_add(interval, function, &data);
> > }
>
> I'm not sure, but this won't work.
> You alloc the data on the stack, register a pointer to this, add leave
> the function destroying this part of the stack. But it also may even
> work in several cases. Those bugs are realy hard to find.
>
> When your timeout occurs, you may get a segfault, because the
> stackframe is no longer valid.
>
> A possible way would be
> {
> struct struct_arg1_arg2 *data;
>
> data = malloc (sizeof (struct struct_arg1_arg2));
> g_assert (data != 0);
> /* init data */
> gtk_timeout_add (interval, function, &data);
> }
>
> function (struct struct_arg1_arg2 *data)
> {
> /* do something with data */
> free (data);
> }
ok, the way I gave won't work but yours won't work too.
because, data will be freed as much as the function is called. (the
function is called periodically with gtk_timeout_add).
then the a better solution is
{
struct struct_arg1_arg2 *data;
data = malloc (sizeof (struct struct_arg1_arg2));
g_assert (data != 0);
/* init data */
gtk_timeout_add (interval, function, &data);
}
function (struct struct_arg1_arg2 *data)
{
/* do something with data */
// no free
}
don't forget to free the data at the end of the program or when the
timeout
callback will be stopped.
--
Hoa
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]