why does this keep crashing

function setup()
    http.request("http://pastebin.com/raw.php?i=YETabpx0",
    function(d) loadstring(d)() setup() end,
        function(e) print(e) end
    )
end

I can’t figure out why this keeps crashing after you swipe between cells about 10 or 12 times and it’s driving me crazy.

Can you be more specific about the crash? Is it making Codea crash or just your program? If the latter, what’s the error message? (If you tap a line in the output pane it gets copied to the clipboard)

Codea crashes.

@simeon @ignatz @dave1707

In the end, nobody is going to know why it crashes, without doing the following - and it should be you that does this.

There is little point printing variable values if Codea crashes as soon as there is a problem, so that won’t work. An alternative is to write variable values to a code tab (or to disk), as a comment string, and save it as you go, so when Codea crashes and you open it again, all the values are there.

What I would probably do, though is strip the app down to the minimum (usually by just commenting out stuff), until it stops crashing, then slowly put the stuff back again piece by piece until you hit the code that is crashing it.

Alternatively, you can add a little test tab where you run bits of your code on their own, to see if they cause a crash.

This sort of thing has happened to me. I have sat for days trying to figure out what is happening, and pretty much every time, the answer is to isolate the problem, by making your app simple and adding back a bit at a time.

Good luck!

@matkatmusic Change your print function to this code. It will save your print statements in reverse order in the tab xxx. It will show your print statements in reverse order, newest print at the top, oldest at the bottom. I only show 400 characters to keep it small. You can increase or decrease that value depending on how much of the print you want to see. Keep adding print statements to your code to figure out where it’s crashing.

oldPrint = print

consoleOutput = {}

function print(...)

    --output.clear()

    local args={...}

    table.insert( consoleOutput,1,table.concat( args, " " ) )

    --oldPrint( table.concat(consoleOutput, "\
" ) )

    str=string.sub(table.concat(consoleOutput, "\
"),1,400)

    saveProjectTab("xxx","--[[\
"..str.."\
--]]")

end

@matkatmusic I added print statements in your function updateExistingLevel and it always crashed after printing “here 1”, but before printing “here 2”. I’m not sure why setContext would be causing a problem. It’s also possible that something in the touch routine could cause the problem since it happens while touching the screen.

function Grid:updateExistingLevel() 

    print( "updateExistingLevel()" )

    self.gridImg = image( 720, 720 )
    
    print("here 1")

    setContext( self.gridImg )
    
    print("here 2")

    for _,c in pairs( self.activeCells ) do

        c:draw()

    end

    setContext() 

    self.shouldUpdateExistingLevel = false

end