Re: g_io_channel_win32_new_messages() & WaitForSingleObject()



On Thu, 02 Apr 2009 19:49:48 -0500
Thomas Stover <thomas wsinnovations com> wrote:
Some notes for anyone else who is just figuring out several things
like myself:
-a "real" new source would have a constructor that would do the 
allocation and setup
-it would also have its own functions for setting up callbacks to
source specific events
-I think g_source_remove_poll() would be called in related
destructor, so I suppose the GPollFD structure needs to be stored in
MySource as well -I guess the "destructor" would be the
closer_callback of the GSourceFuncs parameter
-the GSourceFuncs and GPollFD variables are just copied by glib when 
they given as parameters, so they don't need to dynamic (or 
global/static like my last examples)

I do not really understand what the issues are (that may be more my
problem than yours) but first, yes you generally would store the GPollFD
object in the source object by value if you are making your own custom
polling source, and secondly since you do need to pass g_source_new()
a GSourceFunc struct, the sensible way to implement that is to make the
GSourceFunc object (which is just a struct of function pointers common
to all source objects of that type) a static object - you don't have to,
but it would be pointless doing it any other way because you may have
more than one of the custom source objects in use at any one time which
would otherwise result in duplicated GSourceFunc objects.

Lastly, glib will correctly manage resources if you use the mechanisms
which it provides.  The finalize function you pass to GSourceFuncs
should free any MySource data which happens to have been allocated on
the heap (generally you would not have any).  Everything else will be
taken care of.  In particular g_source_attach() increments the
reference count on the source object, and after you have called
g_source_attach() you can pass ownership of the MySource object to the
main loop in the normal way by calling g_source_unref() on the MySource
source object. The main loop will then automatically destroy your
MySource object if either the dispatch function returns false, or if
g_source_remove() is called on the source id returned by
g_source_attach().  (The function you use to create your custom source
object should return this ID so that users can do just that.)

Chris




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