GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GtkTextBuffer Iterator problem

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
MrGentleman



Joined: 18 May 2007
Posts: 2

PostPosted: Sat May 19, 2007 5:07 am    Post subject: GtkTextBuffer Iterator problem Reply with quote

hello gtk-gurus,

I have a strange problem using the GtkTextBuffer widget. I want to display some text which is provided by two threads in a GtkTextView. The new text simply should be appended to the existing one so that I get a kind of logfile.

Therefore I have created a function called "logging (char* txt_p)" which is called in every thread. This function creates a new text buffer GtkTextBuffer, fetches the current text of the GtkTextView widget, sets an iterator at the end of the text and inserts the new string. Finally, the text buffer is written onto the GtkTextView widget. To make the function thread-safe I use pthread_mutex_t.

Now I have the problem, that the application crashes with the following error:

Gtk-WARNING **: Invalid text buffer iterator: either the iterator is uninitialized, or the characters/pixbufs/widgets in the buffer have been modified since the iterator was created.
You must use marks, character numbers, or line numbers to preserve a position across buffer modifications.
You can apply tags and insert marks without invalidating your iterators,
but any mutation that affects 'indexable' buffer contents (contents that can be referred to by character offset) will invalidate all outstanding iterators

Can anyone help me pleaze??

mike
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 387
Location: Fairfax, Virginia

PostPosted: Tue May 22, 2007 12:00 am    Post subject: Reply with quote

Could you post the code from the function? We can't really tell where you're having the problem without some code ...
Back to top
MrGentleman



Joined: 18 May 2007
Posts: 2

PostPosted: Tue May 22, 2007 5:26 am    Post subject: Reply with quote

Ok, sorry!
The function which adds some text to the textbuffer / textview is the following:

void logging_add (char* text) {

GtkTextBuffer* text_p;
GtkTextIter text_end;
GtkTextView* view_p;

/* Create a new text buffer */
text_p = gtk_text_buffer_new (NULL);

/* Get the text view widget */
view_p = (GtkTextView*)glade_xml_get_widget (xml_p, "txt_logging");

/* Get the current text from the text view */
text_p = gtk_text_view_get_buffer (view_p);

/* Get the last element from the current text buffer */
gtk_text_buffer_get_end_iter (text_p, &text_end);

/* Insert the new text into the text buffer */
gtk_text_buffer_insert (text_p, &text_end, text, -1);

gtk_text_view_set_buffer (view_p, text_p);
}

Always when some data has been arrived, the logging function is called using a critical section for getting the thread-safety:

pthread_mutex_lock (&cs_mutex);
logging_add ("-> ");
logging_add (buffer_head_ptr (&recv_buffer));
logging_add ("\n");
pthread_mutex_unlock (&cs_mutex);

I hope this helps,
mike[/code]
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 387
Location: Fairfax, Virginia

PostPosted: Tue May 22, 2007 1:35 pm    Post subject: Reply with quote

Well, there is no need to call gtk_text_buffer_new(). An initial text buffer is created for you by the GtkTextView, nor is there any need to set the buffer since you retrieved the one that is associated with the GtkTextView.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP