Re: How to use GIOChannel to read an Unix socket



On Tue, 26 Jan 2010 23:59:11 -0800
silverburgh <silverburgh meryl gmail com> wrote:
> I have updated my gio_read_socket per your advice.
> But when the other end closes the socket (the GIOChannel* gio ties to
> that socket),
> I get a segmentation fault.  I think some how when I call
> gio_shutdown, it ends up calling gio_read_socket again, and it crashes
> with a Segmentation fault.
> 
> My console output:
> gio shutdown:
>  gio_read_socket
> 
> (GtkLauncher:5402): GLib-CRITICAL **: g_io_channel_read_line:
> assertion `channel->is_readable' failed
> 
> Program received signal SIGSEGV, Segmentation fault.
> 0x08048aa4 in gio_read_socket (gio=0x8590f50, condition=G_IO_IN,
> data=0x0) at main.c:230
> 230	                g_error ("Error reading: %s\n",
> err->message);
> 
> 
> static gboolean
> gio_read_socket (GIOChannel *gio, GIOCondition condition, gpointer
> data) {
>         printf (" gio_read_socket \n");
>         GIOStatus ret;
>         GError *err = NULL;
>         gchar *msg;
>         gsize len;
> 
> 	if (condition & G_IO_HUP){
> 	  printf ("Read end of pipe died!\n");
> 	  return TRUE;
> 	}
> 
>         ret = g_io_channel_read_line (gio, &msg, &len, NULL, &err);
>         if (ret == G_IO_STATUS_ERROR)
>                 g_error ("Error reading: %s\n", err->message);
>         else if (ret == G_IO_STATUS_EOF) {
>                 printf ("gio shutdown: \n");
> 		g_io_channel_shutdown(gio, true, &err);
> 	} else {
>                 printf ("Read %u bytes: %s\n", len, msg);
>                 g_free (msg);
> 	}
> 
>         return TRUE;
> }
> 
> Thank you for any more help.

You need to return FALSE in the callback if G_IO_HUP or G_IO_STATUS_EOF
is detected, since that will cause disconnection of the callback from
the glib main loop.  If you have previously called g_io_channel_unref()
after the call to g_io_add_watch(), then this will also dispose of the
GIOChannel object, so don't try to access it again.

Chris


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