GLib-CRITICAL Source ID 35 was not found when attempting to remove it



hello,

I have written small piece of code and I am seeing thread_function is getting stuck. If I try to close the window, I am seeing GLib Critical error

Can some body please help me fixing the issue ?

following is the code:

#include<gtk/gtk.h>
#include<cairo.h>
#include<stdlib.h>
#include <pthread.h>

GtkDrawingArea *darea;
GtkVBox  *main_window_box;
GtkWindow    *main_window;

const int size = 500;
int i;

static void my_draw_event( GtkWidget *widget, cairo_t *cr, void *parameter )
{
    int a, b;
    cairo_set_source_rgb( cr, 1, 0, 0 );
    a = random()%size;
    b = random()%size;
    cairo_move_to( cr, a, a );
    cairo_line_to( cr, b, b );
    cairo_stroke(cr);
    gtk_widget_show_all((GtkWidget*)main_window);
    fprintf( stderr, "Values i: %4d a: %3d b: %3d\n", i, a, b );
}

static void *thread_function( void *parameter )
{
    const int seconds = 1000;

    for( i=0; i<seconds; i++ )
    {
        gtk_widget_queue_draw( (GtkWidget*)darea );
        sleep(1);
    }
}

int main()
{
    pthread_t th;

    gdk_threads_init();
    gdk_threads_enter();
    gtk_init( NULL, NULL );

    main_window = (GtkWindow*)gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(main_window),"destroy",G_CALLBACK(gtk_main_quit),NULL);

    main_window_box = (GtkVBox*)gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add (GTK_CONTAINER (main_window),(GtkWidget*)main_window_box);

    darea = (GtkDrawingArea*)gtk_drawing_area_new();
    gtk_box_pack_start( GTK_BOX( main_window_box ), (GtkWidget*)darea, TRUE, TRUE, 0 );
    g_signal_connect( G_OBJECT(darea), "draw", G_CALLBACK( my_draw_event ), NULL );

    gtk_window_set_position(GTK_WINDOW(main_window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(main_window), size, size );

    pthread_create( &th, NULL, thread_function, NULL );
    gtk_widget_show_all((GtkWidget*)main_window);
    gtk_main();
    gdk_threads_leave();

    return 0;
}

Thanks & Regards
--
Lokesh Chakka,
Mobile: 9731023458


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