[gnet-dev] on the handling of refs in gnet
- From: Ian Peters <itp ximian com>
- To: gnet-dev gnetlibrary org
- Subject: [gnet-dev] on the handling of refs in gnet
- Date: 20 Sep 2002 14:19:06 -0400
I've been writing my first program(s) with gnet over the last week, and
I've found the library to be immensely useful. I've also found that it
has some quirks that make it more challenging to work with than it
should. Many of these concern how refs are handled.
For example, gnet_tcp_socket_get_iochannel (aside: why iochannel and not
io_channel, as glib does things?) refs the GIOChannel before returning
it to the caller, which is unconventional (see g_main_loop_get_context
as an example). The most common use of _get_iochannel, as far as I can
tell, will be:
g_io_add_watch (gnet_tcp_socket_get_iochannel (socket), ...)
The watch itself refs the GIOChannel as long as it's in effect (so that
you can handle lifecycle of the GIOChannel entirely by adding a watch,
then unreffing it, and later returning FALSE from the watch function,
which removes the other ref on the GIOChannel, freeing it). Currently
in gnet, you have to save the channel in a local variable, add a watch
on it, and then unref it:
channel = gnet_tcp_socket_get_iochannel (socket);
g_io_add_watch (channel, ...);
g_io_channel_unref (channel);
In other cases, you may be storing the channel locally in some kind of
state. In this case, you explicitly note that you're keeping a ref in
the channel by writing:
state->channel = gnet_tcp_socket_get_iochannel (socket);
g_io_channel_ref (state->channel);
The other interesting case comes up for e.g. asynchronous connect
callbacks. When the user supplied callback function is called, the
socket and inetaddr have a ref count of 1, but the callee is expected to
_unref them if they are unwanted -- rather than being expected to _ref
them if they want to keep them. Ideally, in
gnet_tcp_socket_connect_tcp_cb (tcp.c:150), right after calling
state->func, both the socket and the inetaddr should be unref'd, freeing
them unless the callback function ref'd them explicitly. Otherwise,
most connect callback functions have to gnet_inetaddr_unref the addr,
for example, because few use it outside of the function.
Thoughts?
Ian
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]