Why codea crashes.

What causess codea to crash
… It is the second time it happens to me…

First with set context function and now with table.remove

I would really appreciate a fast answer my due date is sumday and i am not close to ready

@calinefrangieh There’s a lot of things that causes Codea to crash, but they’re mostly coding errors. What I mean by that is you’re doing something that’s causing Codea to use a lot of memory over and over and it eventually runs out and crashes. Do you have setContext or table.remove in the draw function or in a function called from draw. Without seeing code, it’s hard to say what’s happening.

Google “gist”, set up an account, and it is dead easy to store code there and then post the url here.

If you using setContext in draw, then create the image in setup rather than every time you draw. See if this helps

--instead of
img=image(w,h)
setContext(img)
--etc

--try this

--in setup
img=image(w,h)

--in draw
setContext(img)
--etc

This uses the same image for all the setContext drawing.

I will try it in the morning @dave1707
@yojimbo2000 or @Ignatz what is a gits and if you could show me the procedure.
See it has benn long since i programmed, and it was C# so i am kind of back to zero

Yes they were, i used set context first to draw on an image!! Where should i put it ifnot in draw??
And in the second time i used rable.remove…

Thanks @dave1707

You’re going to have to show us some code, otherwise we can’t help. @dave1707 is right though, you probably have a memory leak in your code.

My goal is to show a series of images, on wich i will draw lines.
1-The linecolor should be chosen by pressing the adequate color button.
And be able to draw more than one color. ( i have already been giving codes only parameters are shown in output not in buttons and whenever i change them to fit it loses its ability to save the line on screen)
2- the eraser shouls clean previous drawn lines.
3- there also will be next and previous button and when i press previous i want to be able to see what i drew in the previous scene and be able to change things .
4- i want to save these images and put them on dropbox or send them by mail

@yojimbo2000 the thing is i dont know which part i should send you… If you can tell me wich i will put one instantly

Well, if you don’t mind sharing all the code, that could be simplest. If it’s too long for a comment, you could post the code to a gist and just paste the link here.

If you don’t want to share all of it, my guess is it’s to do with how you’re creating images, so you could share the functions that create the images, and how those functions are called.

@calinefrangieh Try putting collectgarbage() at the beginning of draw() and see if that helps any.

You absolutely need to share code.

Put it in a gist or some other place where you can provide a link to it, then maybe we can help.

https://gist.github.com/calineFrangieh/8d3999148caac92fd78e

I have two questions :
1- is it possible to erase part of the drawing not all of it
2- how do i insert set context and where to save image .

I think you’ll find that the table remove function requires a table index as a parameter, not a vec2

To erase part of a drawing, you can use image:copy to copy a chunk of it to a new image.

If you use the approach I suggested earlier above, you can save your image with saveImage (see Codea reference).

@calinefrangieh I have an app similar to what you were doing. In my app, you use sliders to adjust the pen color and the pen width. As you draw, each draw goes into its own level. By using the reviewLevel slider, you can review each level of your drawing. If you want to remove that level, press the Delete button. After doing all of your drawings, you can save the image to Dropbox by pressing the Save button. You might have to slide the output window down to show all the buttons. I use displayMode(OVERLAY) so you can use the whole screen size if you want. Also, more colors can be added to the color table. If you look this over, you could probably make similar changes to yours.

supportedOrientations(LANDSCAPE_ANY)
displayMode(OVERLAY)

function setup()
    img=image(WIDTH,HEIGHT)
    col={color(0,0,0),color(255,0,0),color(0,255,0),color(0,0,255),
            color(255,255,0),color(255,0,255),color(0,255,255)}
    tab={}
    parameter.integer("drawColor",1,#col,1)
    parameter.integer("penWidth",1,30,5)
    parameter.integer("reviewLevel",0,40,0)
    parameter.action("Delete Level",delete)
    parameter.action("Save",function() saveImage("Dropbox:saveImage",img) print("Image saved") end )
end

function draw()
    setContext(img)
    background(200,200,200)
    if reviewLevel>0 then
        if tab[reviewLevel]~=nil then
            for a2=2,#tab[reviewLevel] do
                stroke(col[tab[reviewLevel][a2].z]) strokeWidth(tab[reviewLevel][a2].w)
                line(tab[reviewLevel][a2-1].x,tab[reviewLevel][a2-1].y,
                                tab[reviewLevel][a2].x,tab[reviewLevel][a2].y)
            end   
        end
    else
        for a1=1,#tab do
            for a2=2,#tab[a1] do
                stroke(col[tab[a1][a2].z]) strokeWidth(tab[a1][a2].w)
                line(tab[a1][a2-1].x,tab[a1][a2-1].y,tab[a1][a2].x,tab[a1][a2].y)
            end
        end  
    end 
    setContext()
    sprite(img,WIDTH/2,HEIGHT/2) 
    fill(col[drawColor])
    noStroke()
    rect(300,HEIGHT-35,30,30)
    ellipse(360,HEIGHT-20,penWidth)
end

function delete()
    if reviewLevel>0 and tab[reviewLevel]~=nil then table.remove(tab,reviewLevel) end
    reviewLevel=0
end

function touched(t)
    if reviewLevel then
        if t.state==BEGAN then drawLevel=#tab+1 tab[drawLevel]={} end
        if t.state==MOVING then table.insert(tab[drawLevel],vec4(t.x,t.y,drawColor,penWidth)) end
        if t.state==ENDED then collectgarbage() end
    end
end

@dave1707 i really appreciate all your help reallyy!!
I cant have sliders in my app! But i used your advice and used setcontext with collectgarbage and it works now… THANK YOU VERY MUCH

BUT i think i will need your code for the saving part? Does this saving savesthe background also ?

saveImage saves whatever you draw to using setContext(name). In my example above, I call background after I do setContext. So whatever my background is will be saved.

Thank you verry much @dave1707 !!
Do you have anyidea about textinput saving?

@calinefrangieh You’ll have to be more specific. Do you want an input box where you key something in and you want to save everything, and then save it where.