Need to free up those precious Texture Memories

Hi I have a question on texture memories. I have written a simple program using the sample code in Corona on button UI.
The program allows me to display images (in this case 3, see code below) by clicking the left and right button. I realised that the images are taking up the texture memories once they are displayed on the screen. I knew the answer is something to do with removeSelf (). Read the tutorial available but found that it is not as simple as it seem.

Will it be possible to remove previous images whenever I click a left or right button??
I hope someone can help me as this is problem has kept me awake for a while now :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
display.setStatusBar (display.HiddenStatusBar)
local lastCheck = {sysMem = 0, textMem = 0}
Runtime:addEventListener('enterFrame', function()
    -- watch for leaks
    collectgarbage()
 
    local sysMem = collectgarbage("count") * 0.001
    local textMem = system.getInfo( "textureMemoryUsed" )*0.000001
 
    if lastCheck.sysMem ~= sysMem or lastCheck.textMem ~= textMem then
        lastCheck.sysMem = sysMem
        lastCheck.textMem = textMem
 
        print ("mem: " .. math.floor(sysMem*1000)*0.001 .. "MB \t " .. math.floor(textMem*1000)*0.001 .. "MB")
    end
end)
local ui = require("ui")
local t = ui.newLabel{
        bounds = { 10, 460, 300, 40 },
        text = "",
        font = "",
        textColor = { 255, 204, 102, 255 },
        size = 18,
        align = "center"
}
 
local p = 0
switchOn = true
local img = {"1.png","2.png", "3.png"}
 
local buttonHandler1 = function( event )
        if event.phase == "release" then                        
                if p>=3  then
                        t:setText ("last picture click left")                   
                elseif event.phase =="release" then
                        p=p+1
                        local image1 = display.newImage(img[p], 0, 0)
                        image1.x=160; image1.y=200
                        switchOn=true
                end
        end
end
local buttonHandler2 = function( event )
        if event.phase == "release" then                        
                if p<=1 then
                        t:setText ("clight right for pic")
                elseif p>=2 and event.id == "button2" then
                        p=p-1
                        local image = display.newImage (img[p],0,0)
                        image.x=160; image.y=200
                        switchOn=true
                end
        end
end
 
 
 
local button1 = ui.newButton{
        default = "btnRight.png",
        over = "btnRightPress.png",
        onEvent = buttonHandler1,
        id = "button1",
        emboss = true
}
 
local button2 = ui.newButton{
        default = "btnLeft.png",
        over = "btnLeftPress.png",
        onEvent = buttonHandler2,
        id = "button2",
        emboss = true
}
 
button1.x = 280; button1.y = 430
button2.x = 190; button2.y = 430

set image1 as a local variable outside of the functions say between line 27-28

27
 local image1 = nil

hi, JayanTV thanks for the tips.
I got it working now after some trial and error. :)
cheers
ben

views:1381 update:2011/10/18 15:01:22
corona forums © 2003-2011