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

Re: Problem using g_io_add_watch



Oups,

I realised that my IO callback function was not well written, so I wrote a
new one.

PROBLEM : Now the problem is that this IO callback is called continuously in
main loop even if nothing
is written in the watched file.

I noticed that if I return FALSE, the callback is called once but is no
longer called on a
write on the watched file.

Should I do some SEEK operation on the file or on the GIOChannel ?

Thanks

Here is the new callback :

======================================================
gboolean
io_callback_end(GIOChannel *source, GIOCondition condition, gpointer data) {
    gchar * str_return;
    gsize length;
    GtkTextIter end;
    GError * error = NULL;
    GtkTextBuffer * buffer = (GtkTextBuffer *)data;

    if(condition == G_IO_IN) {
        GIOStatus status = g_io_channel_read_to_end(source, &str_return,
&length, &error);
        gtk_text_buffer_get_end_iter (buffer, &end);
        gtk_text_buffer_insert(buffer, &end, str_return, length);

        switch(status) {
        case G_IO_STATUS_ERROR:
            g_debug("G_IO_STATUS_ERROR : on_buffer_in");
            break;
        case G_IO_STATUS_NORMAL:
            g_debug("G_IO_STATUS_NORMAL : on_buffer_in %s", str_return);
            break;
        default:
            g_debug("Should not be here : on_buffer_in");
        }

        g_free(str_return);
    }
    return TRUE;
}
======================================================


2008/6/25 sinsedrix <sinsedrix gmail com>:

> Hello,
>
> Problem: When I call g_io_add_watch my application is crashing with no
> explanation.
>
> Here is the shorter file I could write to expose my probem using
> g_io_add_watch :
>
> ==========================================================
> #include <stdio.h>
>
> #include <gtk/gtk.h>
>
> static FILE * MyLogFile = NULL;
>
> void write_in_file(GtkButton * b, gpointer user_data) {
>     static int i = 0;
>     fprintf(MyLogFile, "Something written %d\n", i);
>     i++;
> }
>
> gboolean            on_buffer_in                        (GIOChannel
> *source,
>                                                          GIOCondition
> condition,
>                                                          gpointer data) {
>     gchar ** str_return = NULL;
>     gsize * length = NULL;
>     GtkTextIter end;
>     GError * error = NULL;
>     GtkTextBuffer * buffer = (GtkTextBuffer *)data;
>
>     g_io_channel_read_to_end(source, str_return, length, &error);
>     gtk_text_buffer_get_end_iter (buffer, &end);
>     gtk_text_buffer_insert(buffer, &end, *str_return, *length);
>
>     return FALSE;
> }
>
> void my_exit() {
>     fclose(MyLogFile);
>     gtk_exit(0);
> }
>
> GtkWidget * create_window(void) {
>     GtkWidget *window;
>     GtkWidget *vbox_win;
>     GtkWidget *button;
>
>     GtkWidget *log_view;
>     GtkTextBuffer *log_buffer;
>     GIOChannel * log_channel;
>
>     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
>     gtk_window_set_title (GTK_WINDOW (window), "TestGIO");
>
>     vbox_win = gtk_vbox_new (FALSE, 5);
>     gtk_widget_show (vbox_win);
>     gtk_container_add (GTK_CONTAINER (window), vbox_win);
>
>     log_buffer = gtk_text_buffer_new(NULL);
>     log_view = gtk_text_view_new_with_buffer(log_buffer);
>     gtk_widget_show(log_view);
>     gtk_text_view_set_editable(GTK_TEXT_VIEW(log_view), FALSE);
>     gtk_box_pack_start (GTK_BOX (vbox_win), log_view, TRUE, TRUE, 0);
>
> #ifdef MSDOS
>     log_channel = g_io_channel_win32_new_fd(fileno(MyLogFile));
>     g_debug("Windows Log file %d %p", fileno(MyLogFile), log_channel);
> #else
>     log_channel = g_io_channel_unix_new (fileno(MyLogFile));
>     g_debug("Unix Log file %d %p", fileno(MyLogFile), log_channel);
> #endif
>
>     // The line that crashes :
>     g_io_add_watch(log_channel, G_IO_IN, on_buffer_in, (gpointer)
> log_buffer);
>
>     button = gtk_button_new_with_label("Write something");
>     gtk_widget_show(button);
>     gtk_box_pack_start (GTK_BOX (vbox_win), button, FALSE, FALSE, 0);
>
>     g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK
> (write_in_file), NULL);
>
>     return window;
> }
>
> int
> main (int argc, char *argv[])
> {
>   GtkWidget *window;
>
>   gtk_set_locale ();
>   gtk_init (&argc, &argv);
>
>   MyLogFile = fopen("./MyLogFile.txt", "w+");
>   g_debug("Log file number : %d", fileno(MyLogFile));
>
>   window = create_window ();
>   g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK (my_exit),
> NULL);
>   gtk_widget_show (window);
>
>   gtk_main ();
>   return 0;
> }
> ========================================================
>
> 2008/6/19 Tor Lillqvist <tml iki fi>:
>
> > Problem: When I call g_io_add_watch my application is crashing with no
>> explanation.
>>
>> Can you provide a *minimal* but *complete* sample program (just one
>> source file) that crashes in the same way?
>>
>> > PS: I had a problem with GIOChannel before because I was using Cygwin
>> gcc to
>> > compile (linking with kernel32 and user32) and I read somewhere that I
>> > should use MinGW gcc
>>
>> If you use the Windows GLib packages from ftp.gnome.org, then yes,
>> they are not for use by Cygwin programs, so you should use mingw to
>> compile code that uses them.
>>
>> >  (linking with wsock32) instead, is it right ?
>>
>> wsock32 in particular has little to do with it. Cygwin is just a
>> completely different operating system (even if it happens to run on
>> top of Windows), so you should not use non-Cygwin libraries in Cygwin
>> programs. (Unless in very special cases when you know exactly what you
>> are doing. GLib is not such a case.)
>>
>> --tml
>>
>
>


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