Init function slows down fps

I’m working on a drawing app where you’re able to resize the canvas, and I did this by rerunning the init function for the class that handles the editor. While doing some testing, I noticed that resizing it a lot slowed down my fps and eventually crashed Codea. Is this an issue with Codea, or with my iPad? I’m running just a regular iPad Mini.

I’ve posted some sample code below:

function setup()
    editor = Editor(32,32)
    parameter.watch("1/DeltaTime")
end

function draw()
    background(150)
    editor:draw()
end

function touched(touch)
    editor:touched(touch)
end

Editor = class()

function Editor:init(w,h)
    if w > h then
        self.width = WIDTH*2/3-100
        self.height = self.width*h/w
        self.gridHor = 16
        self.gridVer = 16*h/w
    else
        self.height = HEIGHT - 200
        self.width = self.height*w/h
        self.gridVer = 16
        self.gridHor = 16*w/h
    end
    self.x = (WIDTH*2/3-self.width)/2
    self.y = (HEIGHT-100-self.height)/2
    self.pixel = self.width/self.gridHor

    self.grid = image(self.width,self.height)
    setContext(self.grid)
    noStroke()
    for i = 1,self.gridHor+1 do
        for j = 1,self.gridVer+1 do
            if (i+j)%2 == 1 then
                fill(110, 110, 120, 255)
            else
                fill(180, 180, 190, 255)
            end
            rect((i-1)*self.pixel,(j-1)*self.pixel,self.pixel,self.pixel)
        end
    end
end

function Editor:draw()
    pushMatrix()
    fill(70,70,80)
    noStroke()
    translate(WIDTH/3,50)
    -- rect(0,0,WIDTH*2/3+1,HEIGHT-100)
    translate(Gravity.x*10,Gravity.y*10)
    fill(200)
    translate(self.x,self.y)
    translate(self.width/2,self.height/2)
    sprite(self.grid,0,0)
    popMatrix()
end

function Editor:touched(touch)
    if touch.state == BEGAN then editor:init(8,16)
    elseif touch.state == ENDED then editor:init(16,16)
    end
end

Thanks for the help!

You should turn setContext off when you’ve finished drawing to the image in the init function.

just put setContext() at the end and see if it helps

@Ignatz thanks for pointing that out, but unfortunately I don’t think that solved it. Would having a separate update function help at all? How does it run on your iPad?

No, I think it’s to do with setContext. I haven’t tried it yet (busy)

I’m pretty sure this is a memory management issue to do with the repeated creation of large images. Try to minimise the number of times you create the image (eg you’re doing it on touch began and ended, could it just be done on ended?). Second, add collectgarbage() into the touched function, after you call editor:init().

If the editor class init gets more complex, you might want to consider adding a setSize method that just does the resizing.

@yojimbo2000 thanks for the tip on collectgarbage() I kinda suspected creating the image was causing the problem. Before, I had to resize it maybe 50 times for it to crash, and I probably won’t do that with the finished app

Stress-testing is good practice. Does collectgarbage stop the crashes?

It did, thank you!