Delivered-To: garrigue at math.nagoya-u.ac.jp Delivered-To: lablgtk at yquem.inria.fr Date: Fri, 21 May 2010 13:19:36 +0200 From: Maxence Guesdon To: Alain Mebsout Subject: Re: [Lablgtk] Problem with Gtext.iter in menuitem callback Message-ID: <20100521131936.1da1900b at haddock.home> In-Reply-To: <4BF5541B.6080509 at lri.fr> References: <4BF5541B.6080509 at lri.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: lablgtk at yquem.inria.fr Status: U Le Thu, 20 May 2010 17:24:11 +0200, Alain Mebsout a =C3=A9crit : > Hi lablgtk, > Hello, > I'm trying to insert some text in a source_buffer when clicking on a=20 > menu item (in a popup menu, which shows on right clicking a certain > tag). >=20 > Here is a simplified version of my callback function : >=20 > let triggers_callback (buffer:GSourceView.source_buffer) ~origin:y z > i =3D let ni =3D new GText.iter i in > match GdkEvent.get_type z with > | `BUTTON_PRESS -> > buffer#insert ~iter:ni " | new trigger"; (* THIS WORKS FINE *) > let z =3D GdkEvent.Button.cast z in > if GdkEvent.Button.button z =3D 3 then > let menu =3D GMenu.menu () in > let image =3D GMisc.image ~stock:`ADD () in > let menuitem =3D GMenu.image_menu_item > ~image ~label:"Add trigger(s) ..." > ~packing:menu#append () in > ignore(menuitem#connect#activate > ~callback:(fun () -> > buffer#insert ~iter:ni " | new trigger") > (* GTK ERROR *) > ); > menu#popup ~button:3 ~time:(GdkEvent.Button.time z); > true > else > false > | _ -> false >=20 > The first call to buffer#insert does what it's expected to do but the=20 > second one which is connected to the menu item raises a gtk error > (and sometimes a segfault if I try to access the iter e.g. with=20 > iter#forward_char). It looks like the iter does not exists anymore. I=20 > would be grateful if anybody could give me a hint on whether I'm > missing something here or if there's strange magic happening in menu > items. >=20 > Here is the gtk error triggered by the second call to buffer#insert : >=20 > Gtk-WARNING **: Invalid text buffer iterator: either the iterator is=20 > 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=20 > 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 >=20 > Gtk-CRITICAL **: gtk_text_buffer_insert: assertion=20 > `gtk_text_iter_get_buffer (iter) =3D=3D buffer' failed As the warning says, you're using a deprecated iter. As soon as you change the contents of the buffer, your iter is deprecated and you must create a new one, for example from an absolute position. You may try something like this: after "let ni =3D new GText.iter in", add "let offset =3D ni#offset in" Then, where you used "ni", you can now create a new iter from the abolute offset: let ni =3D buffer#get_iter (`OFFSET offset) in then use this new anv valid iter: buffer#insert ~iter: ni ... (this is not tested but is the general way of working with iters) Regards, Maxence _______________________________________________ Lablgtk mailing list Lablgtk@yquem.inria.fr http://yquem.inria.fr/cgi-bin/mailman/listinfo/lablgtk