Re: [gtk-list] glib problem



On Tue, 15 Feb 2000 13:11:26 +0200 (EET), Giaslas Georgios wrote:
> i want to create a socket and call a function whenever there is something
> to read, using glib. When GIOFunc is set to return FALSE, there is no
> problem with the code, but i don't want the event source to be removed.
> So, i set myfunc to return TRUE but when there is something to read,
> GIOFunc is called once and then i get a segmentation fault.
> Can you help me with this, or suggest something else?

I haven't used the g_io_channel functions myself, but I can give it a try,
they don't look too difficult.

> Here is a part of my code:
> after the creation of the socket i use the following code:
> --------------------------------------------------------
>   myloop=g_main_new(FALSE);
>   mychannel=g_io_channel_unix_new(sockfd);
>   buff="something";
>   g_io_channel_write(mychannel,buff,9,written);
>   eventid=g_io_add_watch(mychannel,G_IO_IN,&myfunc,NULL);
>   g_main_run(myloop);
>   g_io_channel_close(mychannel);
> ---------------------------------------------------------
> gboolean myfunc(GIOChannel *source, GIOCondition condition, gpointer data)
> {
>   gchar *buff;
>   guint *readden;
> 
>   buff=g_malloc(512);

Memory leak! You allocate 512 bytes, but fail to free it again.

>   if (g_io_channel_read(source,buff,100,readden)!=G_IO_ERROR_NONE)

"readden" is an unitialized pointer. It should point to a valid memory
location. I think you actually want:

  guint readden;

  if(g_io_channel(read(source, buff, 100, &readden) != G_IO_ERROR_NONE)

> g_print("failed to read\n");
>   g_print("read : %s\n",buff);
>   if (buff=="something") g_main_quit(myloop);

This will always fail. You are comparing the addresses of buff and
"something", but I think you want to compare the value of the string.
You'll have to use strcmp:

  if(strcmp(buff, "something") == 0)
    g_main_quit(myloop);

Oh, and watch out that buff is NOT a null terminated string, so you have
to terminate it yourself (buff[readden] = '\0' will do the trick).

>   return TRUE;
> }


Erik

-- 
A towel has immense psychological value.
 -- Douglas Adams, The Hitch Hikers Guide to the Galaxy





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