Freeing application class resources...

Hello there!

I’m developing my first game with Corona, and I have some questions about memory usage and disposal.

I’ve created classes for each graphic element of the game (bullets, enemies, etc).

Inside those classes, the actual graphic elements (images, sprites, etc) are created and disposed by constructor and destructor methods that are invoked as game evolves.

The question is about the instances of the classes, given that references are mantained for the graphical elements, but not for them.

For example a class “bullet” that have inside it a graphical image. The image is created and added to screen group on construction method, and is removed from screen group, released (with removeSelf) and set with “nil” on destructor method.

But the “bullet” class itself is created locally by method “fire” and is not hold at any place (like a list), nor is it released after the destructor is invoked.

The question is: do I need to keep a list with the references of all those classes so them don’t get garbage collected while they are still being used (remember that they are created locally and that there is only external references for the graphical elements inside those classes, and not for them)?

Maybe I need to keep this list to have condition to set the reference to “nil” of those classes as well, right?

Thanks a lot and best regards,

Mauro.

The act of having your object in a group is sufficient to hold a reference to it. You don’t need to take it out of the group, just destroy the object and it will clear itself. If the group is destroyed all of its children are destroyed too. 

Hello Rob, and thanks for the reply!

I believe you are talking about screen groups and it’s relationship with screen elements like images and so, but my question is more about the classes that hold the actual screen element.

Let me put some code here to easy the understanding.

Below you can see the constructor and destructor of my “ShipBullet” class (that uses a Class implementation that I’ve found on link http://lua-users.org/wiki/SimpleLuaClasses):

ShipBullet = class( function( s, screenGroup, soundController, hitPoints ) s.screenGroup = screenGroup s.soundController = soundController s.hitPoints = hitPoints s.shipBullet = s.getShipBulletImage() s.shipBullet.anchorX = 0.5; s.shipBullet.anchorY = 0.5; screenGroup:insert( s.shipBullet ) physics.addBody( s.shipBullet, "dynamic" ) s.shipBullet:addEventListener( "collision", function( e ) s:onShipBulletCollision( s.shipBullet, e ) end ) s.shipBullet.self = s end ) function ShipBullet:destroy() print( 'ShipBullet:destroy' ) self.shipBullet:removeEventListener( "collision", function( e ) s:onShipBulletCollision( s.shipBullet, e ) end ) physics.removeBody( self.shipBullet ) self.screenGroup:remove( self.shipBullet ) self.shipBullet:removeSelf() self.shipBullet = nil end

As you can see, the creation and destruction of the “shipBullet” member, that is the bullet image itself, is handled by the constructor and destructor of the “ShipBullet” class. That is also responsible to add and remove it from the screen group, add and remove event listeners, etc.

Regarding the image hold by “shipBullet” class member, I believe is everything ok, but my concern is about the instances of Class “ShipBullet”.

Each instance is created by the method below:

function Starship:fireBullet() local starShipBullet = StarshipBullet( self.screenGroup, self.soundController, self.ship, self.firingTargetX, self.firingTargetY ) end

No reference for the “StarshipBullet” (that is a specialization of “ShipBullet” class) is hold at any place, and when the bullet must be destroyed, I perform the “ShipBullet:destroy()” method, that dispose the image, remove listeners, etc from the Class, but do not perform the disposal of the class instance itself.

I believe, and please correct me if I’m wrong, that I need to hold each “StarshipBullet” instance created in some place (probably a list) and perform the final release (starshipBulletInstance = nill) when the destruction of it happens.

Am I right?

Thanks and best regards,

Mauro.

The act of having your object in a group is sufficient to hold a reference to it. You don’t need to take it out of the group, just destroy the object and it will clear itself. If the group is destroyed all of its children are destroyed too. 

Hello Rob, and thanks for the reply!

I believe you are talking about screen groups and it’s relationship with screen elements like images and so, but my question is more about the classes that hold the actual screen element.

Let me put some code here to easy the understanding.

Below you can see the constructor and destructor of my “ShipBullet” class (that uses a Class implementation that I’ve found on link http://lua-users.org/wiki/SimpleLuaClasses):

ShipBullet = class( function( s, screenGroup, soundController, hitPoints ) s.screenGroup = screenGroup s.soundController = soundController s.hitPoints = hitPoints s.shipBullet = s.getShipBulletImage() s.shipBullet.anchorX = 0.5; s.shipBullet.anchorY = 0.5; screenGroup:insert( s.shipBullet ) physics.addBody( s.shipBullet, "dynamic" ) s.shipBullet:addEventListener( "collision", function( e ) s:onShipBulletCollision( s.shipBullet, e ) end ) s.shipBullet.self = s end ) function ShipBullet:destroy() print( 'ShipBullet:destroy' ) self.shipBullet:removeEventListener( "collision", function( e ) s:onShipBulletCollision( s.shipBullet, e ) end ) physics.removeBody( self.shipBullet ) self.screenGroup:remove( self.shipBullet ) self.shipBullet:removeSelf() self.shipBullet = nil end

As you can see, the creation and destruction of the “shipBullet” member, that is the bullet image itself, is handled by the constructor and destructor of the “ShipBullet” class. That is also responsible to add and remove it from the screen group, add and remove event listeners, etc.

Regarding the image hold by “shipBullet” class member, I believe is everything ok, but my concern is about the instances of Class “ShipBullet”.

Each instance is created by the method below:

function Starship:fireBullet() local starShipBullet = StarshipBullet( self.screenGroup, self.soundController, self.ship, self.firingTargetX, self.firingTargetY ) end

No reference for the “StarshipBullet” (that is a specialization of “ShipBullet” class) is hold at any place, and when the bullet must be destroyed, I perform the “ShipBullet:destroy()” method, that dispose the image, remove listeners, etc from the Class, but do not perform the disposal of the class instance itself.

I believe, and please correct me if I’m wrong, that I need to hold each “StarshipBullet” instance created in some place (probably a list) and perform the final release (starshipBulletInstance = nill) when the destruction of it happens.

Am I right?

Thanks and best regards,

Mauro.