Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Why could this cause a memory leak? — Gideros Forum

Why could this cause a memory leak?

MikeHartMikeHart Guru
edited January 2012 in General questions
Hi folks,

I have a class here, which is called several times in a row. There is this math.random call to determine the duration for the tween before the creation of the tween.
Depending on how I set its parameters, it will cause a memory leak. Any idea why?
 
cParticle = gideros.class(Sprite)
 
local oncParticleTweenCompleteRemove = function(tween)
	local target = tween.target
	target:Remove()
end
 
function cParticle:Remove()
	self.tweenx:setPaused(true)
	local parent = self:getParent()
	self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
	self:removeChildAt(1)
	parent:removeChild(self)
end
 
function cParticle:init(x, y)
	local bitmap = Bitmap.new(texpack:getTextureRegion("particle.png",false))
	bitmap:setAnchorPoint(0.5, 0.5)
	self:addChild(bitmap)
 
	self:setPosition(x, y)
	self:setScale(0.1)
	self:setAlpha(0.8)
	grpFX:addChild(self)
	local tx = x + math.random(-50,50)
	local ty = y + math.random(-50,50)
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
 
 
--************Watch out, the second version to determine the duration for the tween	
--************Will cause a memory leak
	local t =  math.random(5,15)/10
	--local t =  math.random(0.5,1.5)
--************
 
 
	self.tweenx = GTween.new(self, t, {x=tx, y= ty, alpha=0.1, scaleX= 0.8, scaleY = 0.8}, {onComplete=oncParticleTweenCompleteRemove})
 
end
 
function cParticle:onEnterFrame(event)
	--self.angle = self.angle + self.spin
end
Tagged:

Comments

  • GregBUGGregBUG Guru
    edited January 2012
    this one generate correctly a float value from 0.5 to 1.5 ( the duration for the tween)
    local t =  math.random(5,15)/10
    and no memory leak in tween

    this one not...
    	--local t =  math.random(0.5,1.5)
    and generate unexpected values... for tween....

    i think is because in LUA math.random(lower, upper) is designed to work with integers.

    extracted from LUA docs:
    "upper and lower must be integer. In other case Lua casts upper into an integer, sometimes giving math.floor(upper) and others math.ceil(upper), with unexpected results (the same for lower)."

    and (i ask you because i don't know how to check memory), but
    how do you determine memory leaks in Gideros? (lua)...

    thanks.
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • Ouch, you are right. Forgot all about that. :\"> Thank you for bringing me uptodate.

    To check the memory usage, do this:
    print ("memory used:"..collectgarbage("count"))
  • atilimatilim Maintainer
    Hi all,

    Then according to the hint from GregBug, when t = 0, onComplete function is not called. Therefore your particle is not removed from the stage.

    Currently, I'm looking at original GTween. I'll post an update here.

    cheers,
  • atilimatilim Maintainer
    edited January 2012
    Also original GTween doesn't call onComplete when t = 0. Let me contact with the author.

    Edit: An easy solution is adding a very small amount (e.g. 0.00001) to the t.
  • It is just a simple class for my game. Nothing fancy and sorry, there is no particle engine in the works. Maybe later but I first I want to finish the UI plugin and work on this game.

    Likes: atilim

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.