# Avoid crash in recursive Dynamic

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

l=1;
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?

Edit

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,
Infinity}],
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.

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


seems to prevent the crash.

-
You should definitely report the crash to WRI. –  Szabolcs Feb 19 '12 at 19:22
You'll be interested in this: groups.google.com/d/topic/comp.soft-sys.math.mathematica/… –  Szabolcs Feb 19 '12 at 19:35
@Szabolcs Thanks, that must be the same error, and the fix works. Do you want to post this as an answer? –  Jens Feb 19 '12 at 19:41
I can't reproduce it on V9. I'll try later on 9.0.1.0. –  Kuba Mar 25 at 16:45
@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. –  Jens Mar 25 at 16:49

l = 1;

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