GNOME Bugzilla – Bug 606751
Invalid iter when changing a source_buffer during a "changed" event
Last modified: 2014-07-28 08:21:15 UTC
Hello, I encounter the following warning: 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 This happens when I set a callback to a "changed" event of a GtkSourceBuffer and when this callback modifies the contents of the buffer. Here is an example, using the lablgtk2 ocaml bindings. To compile: ocamlopt -o gtk.x -I +lablgtk2 lablgtk.cmxa lablgtksourceview2.cmxa testgtk.ml (You must have ocaml and lablgtk2 installed) Then launch ./gtk.x . Each time a character is entered, the string foo is set as text for the buffer and the warning is written in the terminal. This does not occur with GtkTextBuffer. Regards, Zoggy
Created attachment 151257 [details] The file seems to not have been uploaded durint the initial report.
See that you can NOT work with iters directly, you must save the position of your iter with a mark and get the iter back from the mark when appropiate.
Can you confirm if my previous comment was the issue?
Hello, may be, may be not. Here is the same example with a C program. I associate to the "changed" event on a buffer a function which only inserts "hello" in this buffer, using gtk_text_insert_at_cursor. When inserting, I have the warning of my original post. So I don't use any iter directly, though I imagine that gtk_text_insert_at_cursor does. My guess is that iters are still marked as modified when the callback associated to the "changed" event is called, thus giving the warning. I precise that the text is inserted at the right position (at the cursor position, after the text I type in). The example is compiled with gcc -o testgtk `pkg-config --libs gtksourceview-2.0` `pkg-config --cflags gtksourceview-2.0` testgtk.c and launched with ./testgtk Just type i a letter in the text widget to get the warning. Regards, Zoggy
Created attachment 164880 [details] Example with a C program
Reopening as an example has been provided.
Not a GtkSourceView issue, it happens with a GtkTextBuffer/View too.
You must connect to the "insert-text" or "delete-range" signals, so you can keep the location iter valid.
Great, it works. Thanks !