[Gtk-sharp-list] Gtk.SpinButton recursive signals create segfault

Dan Winship danw@novell.com
Wed, 09 Mar 2005 09:32:34 -0500

On Wed, 2005-03-09 at 00:49 +0100, Fabian Sturm wrote:
> I now produced a proper testcase which compiles and runs.
> If you run it and click on the Spinbutton to increase the value you
> will get a wrong value displayed.
> Why is this so? What is a proper fix to this?

I couldn't explain the exact behavior you're seeing without spending
some time debugging, but I've seen similar problems. (If you hook a
GtkEntry up in basically the same way, then any time you delete a
character, the cursor will jump back to the beginning of the line.)

The problem is that the Data object is (correctly) only emitting Changed
when it actually changes, but the SpinButton is emitting Changed even
when you re-set it to its current value. So you click on the SpinButton,
it emits Changed, then the Data object updates its value and emits its
Changed signal, so then OnDataChanged updates the value of the
SpinButton again, but due to some quirk of how gtkspinbutton is written,
it ends up with the wrong value because it was in an inconsistent state
because it was already in the middle of a change. So then it emits
Changed again, and the Data object updates to the new value, etc etc

Easy enough to fix though:

--- /tmp/spinbutton.cs.orig	2005-03-09 09:17:48.219345843 -0500
+++ /tmp/spinbutton.cs	2005-03-09 09:27:38.794296158 -0500
@@ -54,7 +54,8 @@
     public void OnDataChanged (System.Object o)
-      sb.Value = d.Num;
+      if (sb.Value != d.Num)
+	sb.Value = d.Num;
     // gtk# signal handler

-- Dan