This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jadelade's profile picture

[Lua][Error] CC computers shut down wen i run this program

Started by jadelade, 09 March 2013 - 01:50 PM
jadelade #1
Posted 09 March 2013 - 02:50 PM
I have this program that i'm working on. I added a bit of code to this program just to allow the program to read a folder and replace the numbered id (from the rednet input) with a letter id from the file(from line 9 to line 17). wen i ran the program to debug it the program stoped then after about 7 seconds the computer shut off. all i typed into the file is 17/-/jade. it should create a array (conLs[17] = "jade")


local bottomsave = 25
local w,h = term.getSize()
local savepoint = bottomsave -(h - 2)
local side = {"top","bottom","left","right","front","back"}
local onSides = {}
local contacts = true
local conFolder = "contacts"
local conLs = {}

file = io.open( conFolder, "r" )
local fileCont = file:read()
while fileCont do
  a,b = fileCont:find( "/-/" )
  conLs[ string.sub( fileCont,1,a-1 ) ] = string.sub(fileCont,b +1,#fileCont)
end
file:close()

for i=1,6 do
  if peripheral.getType( side[i] ) == "modem" then
	rednet.open( side[i] )
	table.insert( onSides, side[i] )
  end
end
term.clear()
rednet.broadcast( "Is now online" )
local line = {}
line[bottomsave-2] = "IMC+ bata 1.4 for CraftOS"
line[bottomsave-1] = "Type /clear to clear all text"
line[bottomsave] = "Type /quit to stop IM Pro"
term.setCursorPos( 1,2 )
local function lineMove()
  for n = 1,bottomsave do
	line[n] = line[n+1]
  end
end
local function getName( getID )

end
local function lineUpdate()
  for i = 1,h -2 do
	term.setCursorPos( 1,i+1 )
	term.setBackgroundColor( colors.green )
	term.setTextColor( colors.black )
	term.clearLine()
	print( line[i+savepoint] )
  end
  term.setCursorPos( 1,1 )
  term.setBackgroundColor( colors.lightGray )
  term.setTextColor( colors.black )
  io.write( string.rep( " ",w ) )
  term.setCursorPos( 1,1 )
  io.write( "you are:"..os.computerID() )
end
lineUpdate()
function messageSend()
  while true do
	local Update_Bypass = 1
	term.setCursorPos( 1,h )
	term.setBackgroundColor( colors.lightGray )
	term.setTextColor( colors.black )
	term.clearLine()
	write( "> " )
	local input = io.read()
	if input == "/quit" then
	  term.setBackgroundColor( colors.black )
	  term.setTextColor( colors.white )
	  term.clear()
	  term.setCursorPos( 1,1 )
	  rednet.broadcast( "Is now offline" )
	  for i=1,#onSides do
		rednet.close( onSides[i] )
	  end
	  break
	elseif input == "/clear" then
   Update_Bypass = 0
   for i = 1, bottomsave do
		line[i] = " "
   end
   lineUpdate()
end
	if Update_Bypass == 1 then
	  lineMove()
	  line[bottomsave] = "YOU:"..input
	  lineUpdate()
	  term.setCursorPos( 1,2 )
	  lineUpdate()
	  rednet.broadcast( input )
	  term.setCursorPos( 1,h )
	end
  end
end
function messageReceive()
  while true do
	local id,ms = rednet.receive()
	if id ~= os.computerID() then
	  lineMove()
	  line[bottomsave] = id..":"..ms
	  term.setCursorPos( 1,2 )
	  lineUpdate()
	  term.setCursorPos( 3,h )
	end
  end
end
function scrollsaved()
  while true do
	youDontNeedThis, key = os.pullEvent( "key" )
	if key == 200 then
	  savepoint = savepoint -1
	  if savepoint <= -1 then savepoint = 0 end
	elseif key == 208 then
	  savepoint = savepoint +1
	  if savepoint >= bottomsave -( h -2 ) + 1 then savepoint = bottomsave -(h -2) end  
	end
  lineUpdate()
  end
end
parallel.waitForAny( messageReceive,messageSend,scrollsaved )

thanks for the help
3LiD #2
Posted 09 March 2013 - 03:04 PM
There is nothing to suggest for the computer to shutdown.. Most likely thing to have happened is you haven't set enough ram and the computer has crashed.. ( < For unlikely than likely.. )

I hoped I helped in someway
jadelade #3
Posted 09 March 2013 - 03:06 PM
i give minecraft 10g of ram
MudkipTheEpic #4
Posted 09 March 2013 - 03:54 PM
Parallel sometimes does that when running an erroring function. Try running each individually, just to check.
Engineer #5
Posted 09 March 2013 - 11:48 PM
Off topic, something better you could use for getting the side of a modem:

for k,v in pairs( rs.getSides() ) do
    if peripheral.type( v ) == "modem" then
         rednet.open(v)
         table.insert( onSides, v)
    end
end

Saves you 1 line of code, and this is a little bit more neat :P/>
immibis #6
Posted 10 March 2013 - 12:21 AM
It's because you have an infinite loop.

while fileCont do
  a,b = fileCont:find( "/-/" )
  conLs[ string.sub( fileCont,1,a-1 ) ] = string.sub(fileCont,b +1,#fileCont)
end
AfterLifeLochie #7
Posted 10 March 2013 - 12:48 AM
There is nothing to suggest for the computer to shutdown.. Most likely thing to have happened is you haven't set enough ram and the computer has crashed.. ( < For unlikely than likely.. )

I hoped I helped in someway
Not having enough RAM won't cause computers to power off (usually) - if there was an issue, Minecraft itself would close with an OutOfMemoryException, or the computer would raise an OutOfMemoryException.

The reason this happens is the infinite loop listed above - and as the computer does not yield back to the Java-side of the computer, the computer is forcibly aborted. Computers are expected to yield back (via os.pullEvent(), os.pullEventRaw() or coroutine.yield()) within a specified time period.
KaoS #8
Posted 10 March 2013 - 03:34 AM
Not having enough RAM won't cause computers to power off (usually) - if there was an issue, Minecraft itself would close with an OutOfMemoryException, or the computer would raise an OutOfMemoryException.

I dunno… I have managed to kill CC countless times with messed up loops etc, MC has never crashed for me
Exerro #9
Posted 10 March 2013 - 06:53 AM
I made a wierd program that made every single computer die in the save. If you right clicked on them they wouldn't open and then I made a new world and they didn't work there either. I was trying to do it and made an infinite loop that divided pi by a random number between 1 and 99999 and drew it subbed to 5 chars in 100 different coroutines at once but still…cc is deadly :P/> It's almost always caused by a while loop so try all your functions that have while loops in them on their own and you should find it. If they all work, try printing the variables put into them as you might be putting a 0/something variable in which could cause an error i guess
AfterLifeLochie #10
Posted 10 March 2013 - 11:10 AM
Not having enough RAM won't cause computers to power off (usually) - if there was an issue, Minecraft itself would close with an OutOfMemoryException, or the computer would raise an OutOfMemoryException.

I dunno… I have managed to kill CC countless times with messed up loops etc, MC has never crashed for me

You did not read the paragraph that followed. Loops will cause PC stops because you have not yielded back, created an overflow on the execution stack, or caused some other error which results in the computer terminating itself or being terminated by the Java-side. This is the case here - the code was an infinite loop which did not yield in time.

If, and when, you run out of memory, I guarantee you more than CC will crash.
KaoS #11
Posted 10 March 2013 - 12:53 PM
I'm not sure because in my instance even after breaking the PC that executed the ridiculous loop CC computers no longer worked correctly…
jadelade #12
Posted 10 March 2013 - 01:23 PM
OMG i forgot to put fileCont = file:read() in the loop aaaaa im dumb thanks for all the help