Memory Leak Issue

FYI… print_r function…

[lua]function print_r ( t )
local print_r_cache={}
local function sub_print_r(t,indent)
if (print_r_cache[tostring(t)]) then
print(indent…"*"…tostring(t))
else
print_r_cache[tostring(t)]=true
if (type(t)==“table”) then
for pos,val in pairs(t) do
if (type(val)==“table”) then
print(indent…"["…pos…"] => “…tostring(t)…” {")
sub_print_r(val,indent…string.rep(" “,string.len(pos)+8))
print(indent…string.rep(” “,string.len(pos)+6)…”}")
elseif (type(val)==“string”) then
print(indent…"["…pos…’] => “’…val…’”’)
else
print(indent…"["…pos…"] => “…tostring(val))
end
end
else
print(indent…tostring(t))
end
end
end
if (type(t)==“table”) then
print(tostring(t)…” {")
sub_print_r(t," “)
print(”}")
else
sub_print_r(t," ")
end
print()
end[/lua] [import]uid: 6645 topic_id: 3987 reply_id: 12333[/import]

here is a simpler example. no collisions. take out the line physics.addBody(ball) and the memory leak goes away. otherwise the memory usage gradually counts up

[lua]local physics=require(“physics”)
local rnd = math.random

physics.start()
physics.setGravity(0,0)

local function removeBall(obj)
obj:removeSelf()
end

local function addBall()

local ball = display.newCircle(rnd(200)+50, rnd(200)+50, rnd(50)+5)
ball:setFillColor(128,128,128)
physics.addBody(ball)
transition.to(ball, {time=400, alpha=0, onComplete=removeBall})

end
local function memCheck (event)

collectgarbage(“collect”)
print("System Memory : "…collectgarbage(“count”))
end

local timerBall = timer.performWithDelay(100, addBall, -1)
local timerCheck = timer.performWithDelay( 50, memCheck, -1 )[/lua] [import]uid: 6645 topic_id: 3987 reply_id: 12335[/import]

Do you think there’s a solution to this or is it a bug? [import]uid: 10903 topic_id: 3987 reply_id: 12336[/import]

ansca to confirm bug i think there. my example is simple enough to check whether that memory change is expected and be able to explain why if it is.

[import]uid: 6645 topic_id: 3987 reply_id: 12337[/import]

Shouldn’t you also do

obj=nil

after

obj:removeSelf()

[import]uid: 9371 topic_id: 3987 reply_id: 12344[/import]

As shown by jmp909, the object remains referenced by the eventListener system.
Try removing the object as a listener before calling removeSelf(), and print_r again. [import]uid: 5750 topic_id: 3987 reply_id: 12346[/import]

Also I wonder isn’t the ‘local ball’ still referencing the object hence the leak.
[import]uid: 9371 topic_id: 3987 reply_id: 12348[/import]

Nope, if all references are gone, that “local” is actually the keyword that allows for that variable within that scope to be then collected.
Once you “detach” the imageBall table from the listener and removeSelf(), it should be free to go, there.

Once references are gone, Lua GC will mark the local imageBall variable, in the scope it was created, as trash, and will eventually collect it. [import]uid: 5750 topic_id: 3987 reply_id: 12350[/import]

Out of interest how do you detach the ball from the listener in this example? [import]uid: 9371 topic_id: 3987 reply_id: 12351[/import]

Example of scope in Lua:

[lua]-- global scope

do – first local scope

local var1 = “Within first local scope”

do – second local scope
local var2 = “Out of scope”

do – third local scope
local var3 = “Out of scope”
end
end

print(var1,var2,var3)
end

print(var1,var2,var3)[/lua]

Check out what it prints. [import]uid: 5750 topic_id: 3987 reply_id: 12352[/import]

Out of interest how do you detach the ball from the listener in this example?

That’s pretty straightforward. The obj is the imageBall object which was registered as a table listener by itself. Thus:

[lua]…
if ( obj.y > 315) then
obj:removeEventListener(“collision”,obj) – obj:removeSelf()
obj = nil
end
…[/lua] [import]uid: 5750 topic_id: 3987 reply_id: 12353[/import]

I meant in jmp’s example - post number 21 [import]uid: 9371 topic_id: 3987 reply_id: 12354[/import]

Oh, in that example I’d nil out the object after removeSelf(). There is no object registered as an eventListener.
If that doesn’t solve it, I’d check where the physics is added in the ball table and would apply a removeSelf() method to that object as well.
I haven’t used Game Edition, but let’s say if physics.addBody(ball) adds a ball.physics table, then I’d try:

[lua]if obj.physics.removeSelf then obj.physics.removeSelf() end – just to make sure… if you know it’s got the method, no use for the IF statement![/lua]

But I don’t know if this is actually working that way in Game Edition, nor if the table is called that way… but Ansca says physics objects inherit the removeSelf() method. The point is…where’s the physics object! :wink: [import]uid: 5750 topic_id: 3987 reply_id: 12355[/import]

there’s no such thing as obj.physics

when you nil out the object and print_r(obj) you get nil. but there’s apparently still a memory leak internally. my guess is something in the physics engine is still holding on to a reference. that’s not something we have control of. [import]uid: 6645 topic_id: 3987 reply_id: 12391[/import]

I would forward this issue (as well as a link to this thread) to bugs@anscamobile.com and see if they can confirm a memory leak, and if so, at least it’ll be on their “todo” list of things to take care of for the next release.

If it is a memory leak, great catch! [import]uid: 7849 topic_id: 3987 reply_id: 12568[/import]

I’ve been working on a work around for this issue, but came across another leak issue. Any thoughts would be helpful. Link: http://developer.anscamobile.com/forum/2010/12/30/memory-leak-touch-listener [import]uid: 10903 topic_id: 3987 reply_id: 15281[/import]