Re: GSource object lifetime



Le 03/05/2011 20:49, Thomas Stover a écrit :
trying to stop a memory leak (2.24.1 x86_64)... 

I'm repetitively calling g_idle_source_new(), g_source_set_callback(),
g_source_attach() to get an idle callback to run in a separate thread. The
callback in question always exits with FALSE. 

Don't forget to unref your source after you attached to the context,
because g_source_attach() increases the reference count of the source.

Basically you need to:

s = g_idle_source_new();
g_source_set_callback(s, func, data, notify);
g_source_attach(s, ctx);
g_source_unref(s);

Unless you need to keep a reference to the source for some other reason
of course.

The docs are a bit lacking info on the matter, but the code can tell you
(e.g. the source of g_idle_add(), which actually almost does this with
NULL as the context).

The docs for GSourceFunc() state:
...
Returns :
      it should return FALSE if the source should be removed.
...


Does "removed" mean "frees up the idle source", or just "undoes the
g_source_attach()"? 

AFAIK it means "remove from the context and unref it", so if it was the
last reference to that source it'd be freed, yes.

Should I be trying to reuse the idle source? I was destroying and
recreating a time out source, but finally gave that up and just let it tick
all the time. That stopped a leak in another program so that's why I was
wondering...

I don't think you need to take care of re-using the same source again,
it'd probably be only painful for you and I doubt it has any real benefit.

Cheers,
Colomban



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]