Posted 20 July 2012 - 10:54 PM
I was tasked yesterday with writing a simply clock program for a town nearby where I live in an SMP server. The constraints were simply to have the in-game time write to a monitor in the center of town which would automatically refresh itself and was thus self-sufficient. With a little research, I had all the resources necessary to write such a program. However, about ten minutes later, I was called back to the town, as the clock had stopped refreshing. When I opened up the Console, it showed that the program had crashed, citing some obscure out of bounds exception. Given that this was an actual java error (commonly associated with Minecraft actually crashing), I was unable to find any articles or threads citing the same issue. Confused, I tweaked the program a few times, but kept receiving the error, typically being asked to come back out five to ten minutes later. Frustrated (mostly because I live in the midst of a jungle some three hundred blocks from this town), I went into my single-player test world and tried to replicate the issue. As a perfectionist, I took the time to properly center the clock in the screen, and also tweaked the delay between refreshes so that the clock ran smoothly. In this, I noticed a significantly shorter time between starting the program and it crashing. Here is the error message and initial code:
Error Message on Run via Startup
Error Message on Manually Run
Test Code Mk.I
Curious about the nature of the error, I logged the start and end time four times. The times were as follows:
Original Test Run
I noted here that the program was consistently crashing after between 29 and 30 in-game minutes. Curious, I decided to mess with the one major component from the SMP program I'd written that I had changed: the sleep function.
Test Code Mk.II
Second Test Run
Now I was getting somewhere. The program crashed roughly between 160 and 161 in-game minutes. It wasn't quite directly inverted, but it took almost exactly four times longer to crash when I increased the sleep by five times. Curious about this relationship, I ran another test.
Test Code Mk.III
Third Test Run
In this test, the program consistently after 291 to 292 in-game minutes. I had one more thing I wanted to test. I removed the sleep function altogether, just to see what would happen.
Test Code Mk.IV
Fourth Test Run
The program crashed immediately. Oddly, this was the first instance where any part of the error message changed:
New Error Message on Run via Startup
So, obviously, I can work around this problem by just indefinitely extending the time the computer spends asleep between resets, but this is hardly ideal, given that this is supposed to be a live clock. Thus, I thought that perhaps, with all this information, someone here who knows ComputerCraft and/or lua better than I might be able to suggest why I am encountering this error (such as some syntax formatting quirk) or a possible solution that does not involve ramping the sleep up past 1 second. Hope this information is sufficient.
Error Message on Run via Startup
CraftOS 1.3
bios:15: vm error:
java.lang.ArrayIndexOutOfBoundsException: 256
>
Error Message on Manually Run
CraftOS 1.3
> clock
java.lang.ArrayIndexOutOfBoundsException
>
Test Code Mk.I
mon = peripheral.wrap("back")
mon.setTextScale(2)
orig = textutils.formatTime(os.time(), true)
function time()
mon.clear()
mon.setCursorPos(11,4)
mon.write(orig)
mon.setCursorPos(11,6)
mon.write(textutils.formatTime(os.time(), true))
sleep(0.1)
time()
end
time()
Curious about the nature of the error, I logged the start and end time four times. The times were as follows:
Original Test Run
12:07 - 12:36
13:02 - 13:32
13:43 - 14:12
14:20 - 14:49
I noted here that the program was consistently crashing after between 29 and 30 in-game minutes. Curious, I decided to mess with the one major component from the SMP program I'd written that I had changed: the sleep function.
Test Code Mk.II
mon = peripheral.wrap("back")
mon.setTextScale(2)
orig = textutils.formatTime(os.time(), true)
function time()
mon.clear()
mon.setCursorPos(11,4)
mon.write(orig)
mon.setCursorPos(11,6)
mon.write(textutils.formatTime(os.time(), true))
sleep(0.5)
time()
end
time()
Second Test Run
15:12 - 17:53
18:04 - 20:45
21:08 - 23:49
0:00 - 2:40
Now I was getting somewhere. The program crashed roughly between 160 and 161 in-game minutes. It wasn't quite directly inverted, but it took almost exactly four times longer to crash when I increased the sleep by five times. Curious about this relationship, I ran another test.
Test Code Mk.III
mon = peripheral.wrap("back")
mon.setTextScale(2)
orig = textutils.formatTime(os.time(), true)
function time()
mon.clear()
mon.setCursorPos(11,4)
mon.write(orig)
mon.setCursorPos(11,6)
mon.write(textutils.formatTime(os.time(), true))
sleep(1)
time()
end
time()
Third Test Run
3:04 - 7:56
8:30 - 13:21
13:33 - 18:25
19:20 - 0:11
In this test, the program consistently after 291 to 292 in-game minutes. I had one more thing I wanted to test. I removed the sleep function altogether, just to see what would happen.
Test Code Mk.IV
mon = peripheral.wrap("back")
mon.setTextScale(2)
orig = textutils.formatTime(os.time(), true)
function time()
mon.clear()
mon.setCursorPos(11,4)
mon.write(orig)
mon.setCursorPos(11,6)
mon.write(textutils.formatTime(os.time(), true))
time()
end
time()
Fourth Test Run
0:42 - 0:42
The program crashed immediately. Oddly, this was the first instance where any part of the error message changed:
New Error Message on Run via Startup
CraftOS 1.3
bios:5: vm error:
java.lang.ArrayIndexOutOfBoundsException: 256
>
So, obviously, I can work around this problem by just indefinitely extending the time the computer spends asleep between resets, but this is hardly ideal, given that this is supposed to be a live clock. Thus, I thought that perhaps, with all this information, someone here who knows ComputerCraft and/or lua better than I might be able to suggest why I am encountering this error (such as some syntax formatting quirk) or a possible solution that does not involve ramping the sleep up past 1 second. Hope this information is sufficient.