g_idle_add source & memory leaks



If I use g_idle_add to "send" to my main thread that does the GTK rendering
some notifications I have to free the source handle that the function
returns to me every time or not to avoid a memory leak?

Here is a simple code example, that seems not to leak on linux (ps gives
constant resource occupation, gtk 2.22), but it seems to leak on win32 (task
manager mem usage grows, gtk 2.16):

#include <gtk/gtk.h>

GtkLabel *l;

int mycbk(int val)
{
    char buffer[16];
    sprintf(buffer, "%09d", val);
    gtk_label_set_text(l, buffer);
    return FALSE;
}

void threadfunc(void *unused) {
    int msec = 0;
    while (1) {
        msec++;
        g_usleep(1000);
        g_idle_add((GSourceFunc)mycbk, (void *)msec);
    }
}

int main(int argc, char *argv[]){
    GtkWidget *w;
    g_thread_init(NULL);
    gtk_init(&argc, &argv);

    g_thread_create((GThreadFunc)threadfunc, NULL, TRUE, NULL);
    w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    l = GTK_LABEL(gtk_label_new("-"));
    gtk_container_add(GTK_CONTAINER(w), GTK_WIDGET(l));
    gtk_widget_show_all(w);
    gtk_main();
    return 0;
}


-- 
Ing. Gabriele Greco, DARTS Engineering
Tel: +39-0105761240  Fax: +39-0105760224
s-mail: Via G.T. Invrea 14 - 16129 GENOVA (ITALY)



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