Re: GSourceFuncs.finalize?



On Thu, 2004-04-15 at 05:23, muppet wrote:
> On Tue, 2004-04-13 at 19:35, Alan M. Evans wrote:
> > When does the finalize function in GSourceFuncs get called? The online
> > docs say, "Called when the source is finalized" but never, as best I can
> > tell, describes when that is or even what it means to be "finalized."
> 
> "Finalize" is a sense of "destroy", but not the same thing.  It's what
> happens at the end of the object's lifetime, as opposed to destroy,
> which typically begins that process.  For example, GObject has a
> finalize vfunc which gets called right before the actual instance is
> freed.
> 
> In the case of a GSource, the finalize func is invoked from
> g_source_unref_internal() if the refcount of the object reaches zero --
> just before freeing the memory associated with it.  It is required that
> the source has been removed from its context at that point.

Thanks for that. Very helpful. Please excuse the further inane
questions; I'm trying to write a rather largish app in C, and I think
that the infrastructure provided here by GLib could be of great benefit
to my effort, if I can only wrap my brain around it all at once. (If I
ever do, I will probably compose my notes into a tutorial for future
novices, which others can point such queries to.)

Now then. Supposing I have the following at the heart of main():

    loop = g_main_loop_new(NULL,FALSE);
    source_id = g_source_attach(
        g_source_new(&funcs,sizeof(GSource)),
        g_main_loop_get_context(loop));
    g_main_loop_run(loop);

(Sorry for the code density, I'm trying to make a short example.) When
g_main_loop_run() returns because of some call to g_main_loop_quit(), I
see that my finalize function will get called if I do:

    source = g_main_context_find_source_by_id(
        g_main_loop_get_context(loop),
        source_id);
    g_source_unref(source);

But not if I instead do:

    g_source_destroy(source);

Is that what I'm supposed to expect? Because I wouldn't have based on
your statement that destroy begins the process of an object's
end-of-life, of which finalize is a part.

Is destroy necessary after I do an unref? The online docs seem to
indicate, indirectly, that a destroy would be silly after the GSource
were unref'ed into oblivion.

Also, I feel eerily uneasy calling unref when I've never called ref. In
my mind, new and destroy make a (sort of) decent couplet, as would ref
and unref. But calling new and then unref make me feel like I'm up to no
good.

I guess my real question is: Am I basically getting it? Or am I
following a path that will lead to madness?

-- 
Alan M. Evans <alan extratech com>




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