Memory Leak - Using Wind Vents

When I start level 61, I start experiencing a “leak” when I end the game and replay the current level or end the game and go to the next level.  The leak shows up on my end game screen at the end of level 61.   I do have a “preEndGame” where I am using composer to remove all the scene’s which helped eliminate memory leaks in levels 1-60.   

The only “new” item between level 60 and 61 is the use of wind.   So I am assuming the issue is the addition of wind.  Below is my code.  

Is there something I am not removing that I should be?  Any thing I should do different?

Thanks - I appreciate the help 

Lori

-----------------------------------------------------------------------------

– RETREIVE VENT FORCE VALUES

–the vents angle and desired power

–calculates and returns the proper xF and yF force values that is transferred to to floating objects

-----------------------------------------------------------------------------

local function getVentVals( angle, power )

      local xF = math.cos( (angle-90)*(math.pi/180) ) * power

      local yF = math.sin( (angle-90)*(math.pi/180) ) * power

      return xF,yF

end

-----------------------------------------------------------------------------

– VENT COLLISION HANDLER

-----------------------------------------------------------------------------

local function ventCollide( self,event )

   local vent = event.other

   

   if ( event.phase == “began” and vent.isVent == true ) then

         self.xF = self.xF+vent.xF ; self.yF = self.yF+vent.yF

   elseif ( event.phase == “ended” and vent.isVent == true ) then

         self.xF = self.xF-vent.xF ; self.yF = self.yF-vent.yF

   end

end

-----------------------------------------------------------------------------

– RUNTIME FORCE APPLICATION

-----------------------------------------------------------------------------

local function constantForce1()

   if not (tableBumper3.xF == 0 and tableBumper3.yF == 0 ) then

       tableBumper3:applyForce(tableBumper3.xF,tableBumper3.yF,tableBumper3.x,tableBumper3.y )

   end

end

local function constantForce2()

   if not (tableBumper4.xF == 0 and tableBumper4.yF == 0 ) then

       tableBumper4:applyForce(tableBumper4.xF,tableBumper4.yF,tableBumper4.x,tableBumper4.y )

   end

end

-------------------------------------------------------

–VENT REMOVAL

-------------------------------------------------------

tableBumper3:removeEventListener( “collision”, tableBumper3)

Runtime:removeEventListener( “enterFrame”, constantForce1 )

tableBumper4:removeEventListener( “collision”, tableBumper4)

Runtime:removeEventListener( “enterFrame”, constantForce2 )

Hi Lori,

Assuming you added the collision listeners like this…

[lua]

tableBumper3.collision = ventCollide

tableBumper3:addEventListener( “collision”, tableBumper3 )

[/lua]

Consider setting “.collision” to “nil” like this, upon removal:

[lua]

tableBumper3:removeEventListener( “collision”, tableBumper3 )

tableBumper3.collision = nil

[/lua]

Also, may I ask if this scenario involves basically one “main object” which collides with many other things, including vents? If so, you should change things over so that only that main object has the collision listener on it, and everything else is handled via the “other” object involved in the collision.

Brent

I’ll give that a try.  Thanks

Brent,

I added the nil line to collision event listener and I actually went back and added to all my event listeners on all 150 levels as a precaution.  That helped alot.  So I dug a little deeper and noticed an issue on my dump module.  I was “requiring” modules that I did not need too.  When I removed them everything worked as expected and the remainder of the memory problem was resolved.

I appreciate the help and guidance.

Lori

Can I just ask something based on your last post?  

Do you have all of your 150 levels separated into their own lua file? Presumably with (roughly) the same code in each?  

I only ask because if you have then I’m sure you’ll find that making a single small change can become really time consuming, as it’s duplicated 150 times. It might be worth looking into making a single “game.lua” file, which uses some json data or something when it loads which defines the layout of that level. That way the core functionality is just in one file, and you don’t have to worry about things like adding a fix to 149 out of your 150 levels and then shipping with one broken level.

Yes each is in a seperate lua file.  I guess that is a newbie error.   Next time I much prefer your recommendation.  However, the core functionality does change as the player increases in levels.  The change in layout is the result of changes in the core functionality.  Originally I started with everything in one lua file and became overwhelmed - probably because I was learning to program for the first time.

Thanks for the tip!  

Lori

I realise that when you’re at a late stage in development it’s basically impossible to make big changes like this.  

In future (or for anyone else who is curious), if you have groups of levels with similar functionality you could always have a lua file for each level type. E.g:

First we’ll create a file that holds data for each level (I’ll just create one for platform levels, one for driving levels, and one for swimming levels)

--leveldata.lua local layoutData = { [1] = { object = { x= 0, y= 0}, platforms = { { w= 100, h= 100}, } }, [2] = { object = { x= 50, y= 50}, cars = { { w= 100, h= 100}, { w= 150, h= 150}, } }, [3] = { object = { x= 0, y= 0}, fish = { {w= 100, h= 100}, {w= 200, h= 250}, }, }, }

and then:

platformLevels.lua local function createScene(levelNumber) local levelData = json.decode("levelData") --let's assume levelNumber has been set to 1 somewhere local dataForThisLevel = levelData[levelNumber] --setup the object local object = display.newImageRect("object.png", dataForThisLevel.object.x, dataForThisLevel.object.y) --setup the things for i = 1, #dataForThisLevel.platforms do local myThing = display.newImageRect("platforms.png", dataForThisLevel.platforms[i].x, dataForThisLevel.platforms[i].y) end end

swimmingLevels.lua local function createScene(levelNumber) local levelData = json.decode("levelData") --let's assume levelNumber has been set to 3 for this level local dataForThisLevel = levelData[levelNumber] --setup the object local object = display.newImageRect("object.png", dataForThisLevel.object.x, dataForThisLevel.object.y) --setup the things for i = 1, #dataForThisLevel.fish do local myFishy = display.newImageRect("fish.png", dataForThisLevel.fish[i].x, dataForThisLevel.fish[i].y) end end

Not a perfect example, but hopefully it gives people an idea  :slight_smile:

THANKS!   That will be so helpful for my next project.  

Alan, (or Corona Staff)

Could we get a tutorial out of the level data tips here?

It’s not really within the topic of the post but, I feel, would be useful to a lot of people not necessarily interested in the original thread.

Jules.

I wouldn’t mind writing one, the only problem is that my workload is pretty heavy right now so I can’t give a timeframe for when I would be able to do it.

Hi Lori,

Assuming you added the collision listeners like this…

[lua]

tableBumper3.collision = ventCollide

tableBumper3:addEventListener( “collision”, tableBumper3 )

[/lua]

Consider setting “.collision” to “nil” like this, upon removal:

[lua]

tableBumper3:removeEventListener( “collision”, tableBumper3 )

tableBumper3.collision = nil

[/lua]

Also, may I ask if this scenario involves basically one “main object” which collides with many other things, including vents? If so, you should change things over so that only that main object has the collision listener on it, and everything else is handled via the “other” object involved in the collision.

Brent

I’ll give that a try.  Thanks

Brent,

I added the nil line to collision event listener and I actually went back and added to all my event listeners on all 150 levels as a precaution.  That helped alot.  So I dug a little deeper and noticed an issue on my dump module.  I was “requiring” modules that I did not need too.  When I removed them everything worked as expected and the remainder of the memory problem was resolved.

I appreciate the help and guidance.

Lori

Can I just ask something based on your last post?  

Do you have all of your 150 levels separated into their own lua file? Presumably with (roughly) the same code in each?  

I only ask because if you have then I’m sure you’ll find that making a single small change can become really time consuming, as it’s duplicated 150 times. It might be worth looking into making a single “game.lua” file, which uses some json data or something when it loads which defines the layout of that level. That way the core functionality is just in one file, and you don’t have to worry about things like adding a fix to 149 out of your 150 levels and then shipping with one broken level.

Yes each is in a seperate lua file.  I guess that is a newbie error.   Next time I much prefer your recommendation.  However, the core functionality does change as the player increases in levels.  The change in layout is the result of changes in the core functionality.  Originally I started with everything in one lua file and became overwhelmed - probably because I was learning to program for the first time.

Thanks for the tip!  

Lori

I realise that when you’re at a late stage in development it’s basically impossible to make big changes like this.  

In future (or for anyone else who is curious), if you have groups of levels with similar functionality you could always have a lua file for each level type. E.g:

First we’ll create a file that holds data for each level (I’ll just create one for platform levels, one for driving levels, and one for swimming levels)

--leveldata.lua local layoutData = { [1] = { object = { x= 0, y= 0}, platforms = { { w= 100, h= 100}, } }, [2] = { object = { x= 50, y= 50}, cars = { { w= 100, h= 100}, { w= 150, h= 150}, } }, [3] = { object = { x= 0, y= 0}, fish = { {w= 100, h= 100}, {w= 200, h= 250}, }, }, }

and then:

platformLevels.lua local function createScene(levelNumber) local levelData = json.decode("levelData") --let's assume levelNumber has been set to 1 somewhere local dataForThisLevel = levelData[levelNumber] --setup the object local object = display.newImageRect("object.png", dataForThisLevel.object.x, dataForThisLevel.object.y) --setup the things for i = 1, #dataForThisLevel.platforms do local myThing = display.newImageRect("platforms.png", dataForThisLevel.platforms[i].x, dataForThisLevel.platforms[i].y) end end

swimmingLevels.lua local function createScene(levelNumber) local levelData = json.decode("levelData") --let's assume levelNumber has been set to 3 for this level local dataForThisLevel = levelData[levelNumber] --setup the object local object = display.newImageRect("object.png", dataForThisLevel.object.x, dataForThisLevel.object.y) --setup the things for i = 1, #dataForThisLevel.fish do local myFishy = display.newImageRect("fish.png", dataForThisLevel.fish[i].x, dataForThisLevel.fish[i].y) end end

Not a perfect example, but hopefully it gives people an idea  :slight_smile:

THANKS!   That will be so helpful for my next project.  

Alan, (or Corona Staff)

Could we get a tutorial out of the level data tips here?

It’s not really within the topic of the post but, I feel, would be useful to a lot of people not necessarily interested in the original thread.

Jules.

I wouldn’t mind writing one, the only problem is that my workload is pretty heavy right now so I can’t give a timeframe for when I would be able to do it.