With the following Dynamic cell, I can reproducibly crash Mathematica, so do not try this unless you have saved all your work!

Dynamic[Sin[l = l + 1]]

This will harmlessly run forever, or until you try to right-click on the dynamic output (to get the context menu and perhaps to copy the current value). When you right-click on it, Mathematica crashes.

Is there a simple way to avoid or catch this error before it occurs?


I also see this crash when literally using the example from the documentation for Dynamic (under "Applications"):

{Trigger[Dynamic[x, (a = FractionalPart[#]; x = Round[#]) &], {0, 
 Dynamic[{Quotient[x, 60], Mod[x, 60], SetPrecision[10 a, 2]}]}

and again trying to right-click. So it's not like I'm doing something outlandish here.

Edit 2 The link Szabolcs found does indeed answer my question:

EventHandler[Dynamic[Sin[l = l + 1]], {"MouseUp", 2} :> Null]

seems to prevent the crash.

  • $\begingroup$ You should definitely report the crash to WRI. $\endgroup$ – Szabolcs Feb 19 '12 at 19:22
  • $\begingroup$ You'll be interested in this: groups.google.com/d/topic/comp.soft-sys.math.mathematica/… $\endgroup$ – Szabolcs Feb 19 '12 at 19:35
  • $\begingroup$ @Szabolcs Thanks, that must be the same error, and the fix works. Do you want to post this as an answer? $\endgroup$ – Jens Feb 19 '12 at 19:41
  • $\begingroup$ I can't reproduce it on V9. I'll try later on $\endgroup$ – Kuba Mar 25 '14 at 16:45
  • $\begingroup$ @Kuba, yes, I think this was fixed in the meantime - I don't see the crash on 9.0.1 anymore. But in your question here, the fix seems to work. $\endgroup$ – Jens Mar 25 '14 at 16:49

Based on this MathGroup post describing a similar crash, the right click can be caught and discarded like this:

l = 1;
EventHandler[Dynamic[Sin[l = l + 1]], {"MouseUp", 2} :> Null]

This won't let you copy the value, but it will prevent crashes due to accidental right clicks.

You can obtain the value by evaluating l as a different input.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.