Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
again memory leak in my code... but why... — Gideros Forum

again memory leak in my code... but why...

GregBUGGregBUG Guru
edited February 2012 in General questions
sorry but i can't understand why there is a memory leak in this little code:
collectgarbage()
s = collectgarbage("count")
 
--emittersInit()
--particlesInit()
 
local particleGFX = (Texture.new("gfx/smoke.png"))
--emitter1 = emittersCreate("emitter1", 160, 240, 0, stage)
--test1 = particlesCreate("test1", particleGFX, 50, 3)
 
 
u =  collectgarbage("count")
--emittersFree()
--particlesFree()
--emitter1 = nil
--test1 = nil
particleGFX = nil
collectgarbage()
e = collectgarbage("count")
 
print("----- Memory Userd: "..(u*1024).." bytes")
if e-s > 0 then
	print("----------- MEMORY LEAK: "..((e-s)*1024).." bytes")
else	
	print("----------- MEMORY OK")
end
exactly 48 bytes...

:(

i'm going crazy...

but how it work garbage collector in LUA ?
TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
www.tntengine.com

Comments

  • You are defining these global variables s, u, and e. The some memory too, you know.
    Plug I am not sure if somethign is collected right away with a collectgarbadge directly after the nil, maybe it is. Try to define these global variable up front before the frist counting.
  • The collectgarbage after a nil doesn't always catch it up immediately.
    But I wouldn't bother much with those small leaks honestly, as long as they don't get out of hand.
    Just avoid the use of globals as much as you can d:
  • GregBUGGregBUG Guru
    edited February 2012
    thanks Mike for your quick response!

    i tried to define s,u,e before first counting but is the same always 48 bytes lost...
    removed u variable and always 48 bytes lost... :(

    all my code (commented in this test) work fine and no memory leak...

    but when i add the line
    local particleGFX = (Texture.new("gfx/smoke.png"))
    and then a free with
    particleGFX = nil
    i have the memory leak... 48 bytes (152byte if i not nil patricleGFX)
    could be that i wrong free the texture ?
    or the garbage collector that works different and defers the release of "particleGFX" ?
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • H4ch1H4ch1 Member
    edited February 2012
    Ok, this code here doesn't leak to me (: (or better, it does only once, then memory gets ok.)
    So problem seems definitely calling collectgarbage right after nilling.
     
    collectgarbage()
    s = collectgarbage("count")
     
    --emittersInit()
    --particlesInit()
     
    local particleGFX = Texture.new("0.png");
    --emitter1 = emittersCreate("emitter1", 160, 240, 0, stage)
    --test1 = particlesCreate("test1", particleGFX, 50, 3)
     
     
    u =  collectgarbage("count")
    --emittersFree()
    --particlesFree()
    --emitter1 = nil
    --test1 = nil
    particleGFX = nil
    collectgarbage()
    e = collectgarbage("count")
     
    local function enterFrame()
    	collectgarbage()
    	e = collectgarbage("count")
    	if e-s > 0 then
    		print("----------- MEMORY LEAK: "..((e-s)*1024).." bytes")
    	else	
    		print("----------- MEMORY OK")
    	end
    end
     
    stage:addEventListener(Event.ENTER_FRAME, enterFrame, stage);
     
    print("----- Memory Userd: "..(u*1024).." bytes")
    if e-s > 0 then
    	print("----------- MEMORY LEAK: "..((e-s)*1024).." bytes")
    else	
    	print("----------- MEMORY OK")
    end
  • @h4ch1
    thanks!!! :)
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • @ar2rsawseen
    Yes, i'm a little bit paranoic! :P
    :D
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • atilimatilim Maintainer
    edited February 2012
    please be paranoic :) After each message like yours, I double check the corresponding part of the code. And sometimes there can be very sneaky bugs and so that I can catch them :-B
Sign In or Register to comment.