[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Reminder on tl_delete and text_delete



PureBytes Links

Trading Reference Links

I know this is arcane...

Three days ago, for no apparant reason, my TS2000i started crashing 
intermittently, with different errors but usually "illegal instruction" 
or "out of global memory". One time, a single chart vanished, but the 
others continued behaving correctly! Yet when I rebooted and reopened 
the same workspace it was fine and ran error-free. At least for a while. 
Worse, it only happened when the market was open (and getting realtime 
ticks), so debugging cut into trading time. At one point it was crashing 
every ten minutes. I couldn't imagine anything I'd changed in my code or 
workspace that could have had such catastrophic results. I kept 
thinking, "I haven't changed anything..." -- well, anything 
'significant' anyway.

To make a long story short, I finally isolated the problem to the way I 
use the text_delete statement.

My code displays entry/target/stop prices (among other things) on the 
chart based on my criteria. But to reduce clutter it only displays a few 
pieces of current information, which are recalculated and repositioned 
periodically. A typical code fragment looks like this

var: han(-1); { use -1 because 0 might be a valid handle }
if han > -1 then
    text_delete(han); { only delete if text exists }
han = text_new(...);

This has worked perfectly fine since the text handling capabilities were 
released.

But recently I wanted to reduce clutter even further, so I added some 
more conditions that turned text off from elsewhere in the code. And I 
used the same "if ... then text_delete()" logic. That's when the 
intermittent crashing started. (Right, "I haven't changed anything", LOL)

It turns out that from time to time, the same text could be deleted by 
different conditions. And here's the problem: text_delete(han) works 
fine the first time, but blows up the second time, presumably because 
the memory allocated to the orignal text is now used by something else. 
This is a TS2000i bug, but it explains why it crashed in a variety of 
ways -- the type of crash (or even if it crashed at all) depended on 
what random memory chunk got deallocated or overwritten.

The solution to this (and this is my reminder -- don't let this happen 
to you) is to reset the handle when its not in use:

var: han(-1); { use -1 because 0 might be a valid handle }
if han > -1 then begin
    text_delete(han); { only delete if text exists }
    han = -1; { reset handle to show text does not exist }
end;
han = text_new(...);