Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Class inheritance and memory — Gideros Forum

Class inheritance and memory

gianmichelegianmichele Member
edited July 2012 in General questions
Hi all, I have a quick question: I have a class (Class A) that creates three sprite objects through another class (Class B).

How should I approach memory cleaning when I instance Class A in my game loop but then delete all of the children of Class A manually?

Likes: bdsinger, Daimyo21

Dislikes: myrain25

+1 -1 (+2 / -1 )Share on Facebook

Comments

  • If you're worried about niling out all the sprites within Class A, you could create a destroy function that you call to remove it. This way within that function you could do any necessary cleanup.
  • Better if I post something:

    item.lua
    Bello = Core.class(Sprite)
     
    function Bello:init(texture)
     
    	local bitmap = Bitmap.new(Texture.new(texture))
    	bitmap:setAnchorPoint(0.5, 0.5) -- center the pivot point
    	self:addChild(bitmap) -- aggiunge la bitmap all'oggetto cosi da poterlo manipolare con self
    	self.isFocus = false
     
    	self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
    	self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
    	self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
    end
     
    function Bello:onMouseDown(event)
    	if self:hitTestPoint(event.x, event.y) then
    		self.isFocus = true
     
    		self.x0 = event.x
    		self.y0 = event.y
     
    		print("Touched!")
     
    		event:stopPropagation()
    	end
    end
     
    function Bello:onMouseMove(event)
    	if self.isFocus then
    		local dx = event.x - self.x0
    		local dy = event.y - self.y0
     
    		self:setX(self:getX() + dx)
    		self:setY(self:getY() + dy)
     
    		self.x0 = event.x
    		self.y0 = event.y
     
    		event:stopPropagation()
    	end
    end
     
    function Bello:onMouseUp(event)
    	if self.isFocus then
    		self.isFocus = false
    		event:stopPropagation()
    		print("Touch is ended")
    	end
    end

    mechanic.lua
     
    -- Multi elements + death timer
     
    Trio = Core.class(Sprite)
     
    function Trio:init(texture, n)
     
    	self.elements = n
    	self.isAlive = true
     
    	for j = 1,n do
    		gameobj[j] = Bello.new(texture)
    		gameobj[j]:setPosition(math.random(20,420), math.random(20, 320))
    		self:addChild(gameobj[j])
    	end
     
    	local timer = Timer.new(5000, 1)
    	local function onTimerComplete(event)
    		print("Timer has ended!")
     
    		for j = self:getNumChildren(), 1,-1 do
    			self:removeChildAt(j)
    		end
     
    		self.isAlive = false 
    		print(self.isAlive)
    		print(self:getNumChildren())
     
    	end
    	timer:addEventListener(Event.TIMER_COMPLETE, onTimerComplete)
    	timer:start()
     
    	print(self:getNumChildren())
     
    end
    main.lua
     
    -- Test meccanica di gioco
     
    gameStage = Sprite.new() -- Game group
     
     
    gameobj = {} -- game objects
     
     
    myMech = Trio.new("images/penguin.png", 3)
    gameStage:addChild(myMech)
     
     
    stage:addChild(gameStage)
  • I'm not really sure to this extent would be necessary, but to show the concept of what I meant with a destroy function to be used as cleanup:
    function Bello:destroy()
            local bitmap = self:getChildAt(0) -- Since it seems to only have one child
            self:removeChild(bitmap)
            bitmap = nil
    	self.isFocus = nil
     
    	self:removeEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
    	self:removeEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
    	self:removeEventListener(Event.MOUSE_UP, self.onMouseUp, self)
     
    end
    And then in your Trio class, you could change the one loop to add in a destroy call before removing them:
    local function onTimerComplete(event)
    		print("Timer has ended!")
     
    		for j = self:getNumChildren(), 1,-1 do
                            self:getChildAt(j):destroy()
    			self:removeChildAt(j)
    		end
     
    		self.isAlive = false 
    		print(self.isAlive)
    		print(self:getNumChildren())
     
    	end
  • ar2rsawseenar2rsawseen Maintainer
    edited July 2012
    Hello,

    Here is the way I understand it

    Let's look at this hierarchy:

    Rectangles representing objects and arrows representing reference.

    image

    As you may know, when object has no references it can be garbage collected.

    So let's see:

    If you remove gameStage from stage, then gameStage does not have any references left to it, so it will be garbage collected.
    stage:removeChild(gameStage)
    Variable myTech still will be referencing to Trio instance, and since myTech is aglobal object (I didn't see any definition of local myTech) thus Trio will remain in the memory and everything else also.

    So next step, you nil out the myTech variable
    myTech = nil
    And here goes the domino effect:

    It means there are no references to Trio instance, so it will also be garbage collected.

    Which means that there are no references left to all Bello instances, so they also will be garbage collected.
    Same goes to bitmap. We had a local variable inside Bello class and we also added same bitmap as a child of Bello instance. You could think that it's two references, but, when Bello instance is garbage collected, local variable bitmap will also be removed automatically.

    That is why it's important to use local variables and not global ones, unless you know what you're doing

    So all that is left is a Bitmap instance without parent (Bello was removed), which means there are no references to it, which means it will also be garbage collected

    So basically minimum what you need to do is to remove gameStage from stage and nil out the myTech variable

    Hope that clears something up

    And please correct me if I'm wrong :)

    P.S hopefully that will make a nice post for my 600th forum post :D
    Drawing0.png
    404 x 444 - 11K
  • Thanks everyone, this makes perfectly sense now (and you know, I'm an animator not a programmer!)

    Likes: gianmichele

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