On 4 December 2013 13:31,  <jcup...@gmail.com> wrote:
> Here's a tiny, complete program that does almost what you want. It's
> gtk2, but should work fine with gtk3.
>
> http://pastebin.com/PsG2UDkY
>
> It just updates a status bar, but it'd be easy to make it do a textview 
> instead.
>
> John

===========================

Hi John,

Thanks for this example. It does appear to be what I want to do. I'll play with 
it tonight. 
It seems to be precisely what I want to do.

The constraints within which I must work are:

A thread (not main iteration) does stuff that generates messages which need to 
be periodically added to a textview in the UI - sometimes quickly.

I need to use an idle function to update the UI, so the thread calls 
g_idle_add().

Function g_idle_add() only accepts a *single* pointer as data for the idle 
function. 

The idle function needs a pointer to the textview I want messages to appear in.

Passing both a pointer to the textview *and* a message requires packing both 
into a struct.

I want the idle function to have its own copy of the message in memory so that 
the next message can be prepared in the thread without damaging the message the 
latest idle function is currently posting to the textview. srtrdup() seems to 
be the ticket.

I need then, to use strdup() and still have a pointer to the struct as argument 
to g_idle_add().

To demonstrate this passing of data, I had created the example I gave. It seems 
to work fine and Valgrind reports no read/write violations or memory leaks.
-------

Things I've learned yesterday are:

1. strdup() (I've never seen or used it before)
2. what the heck heap and stack mean (still more to learn there)
3. a more general and flexible solution is probably to use asynchronous message 
queuing

and today you have shown me, not only g_new() (never seen that one before), but 
also a working example. Awesome!

Two questions though:

I assume these would be roughly equivalent:

msg = g_new (Msg, 1);

versus

msg = (Msg *) malloc (1 * sizeof (Msg));

The GTK+ documentation:

http://www.gtk.org/api/2.6/glib/glib-Memory-Allocation.html#g-new

...doesn't explicitly say it allocates on the heap, but I see you free it in 
the idle function that updates the UI, so it must be so. i.e., g_new() must use 
malloc() and not alloca().

Second, why use the sleep (1), or is that just so we can see things happen? 
i.e., so the whole show isn't over in a millisecond?

Thanks for everybody's help and advice.

Dave
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Reply via email to