Game Development Community

iTorque 1.5 Memory Leak Fixes for Novices

by Watermark · in iTorque 2D · 08/03/2012 (6:43 pm) · 4 replies

Hello everyone. My iTorque game crashes after some time playing. This is a serious issue affecting my current game ratings and future games I plan to make with iTorque. I assume this is caused by memory leaks. As I only know script and am a complete novice in C the ability to find the error in the engine is beyond me. I am sure there are others like myself: we use iTorque because we do not have ability to write a complete game in C.

After running forum search, it appears that there are various memory problems with iTorque engine, in either schedule, collisons, or PUAP? However, a lot of threads are outdated and don't have the solution posted. As my C programming skills are quite basic, I don't even know where to start looking for fixing this. And I couldn't find any solutions running forum search. As the next update looks to be quite a while yet, I ask if either GG or knowledgable experts can light the way for us novices. Mainly:

1. Which functions have known serious leaks and we should probably avoid?
2. Are there existing solves for iTorque 1.5? Where in the engine we can change? If so, please point to the post to look.
3. Is there a ForceGarbageCollect function somewhere we can call? I am thinking at worst this is something I can call in code from time to time if the leaks can't be stopped.

Thank you!

About the author

Three iTorque 1.5 games published: Sorceria 1: The Mad Doctor RPG Sorceria 2: Sunken City Sagas: RPG Boardgame and Name Generator For more info see our site: http://wmrpg.weebly.com


#1
08/06/2012 (12:15 am)
When I was first using iTorque I found my biggest memory leak was not understanding how the image datablocks work. If you use the builder defaults, the setting is to preload = "1" and allowUnload = "0". These settings ensure that images get loaded with the level and don't get unloaded if you switch levels.

#2
08/06/2012 (7:42 am)
Thanks Paul! So it would be better to set allowUnload = "1" to allow cleaning after each level? This would insure memory gets released?

I got another question that I was wondering if anyone can tell me the answer: are ScriptObjects automatically deleted? See, my RPG game crashes after running Battle 13 times. I thought maybe it's because I am not deleting new enemies I created. So I added these lines when battle ends:

while($gm.battleSetEnemy.getCount() > 0)
{
%tBattler = $gm.battleSetEnemy.getObject(0);
%tBattler.delete();
}

Where %tBattler is a ScriptObject. This loops through all the enemies I created and deletes them. Strangely, this doesn't seem to improve the situation at all.

I read in some thread that ScriptObjects or anything that isn't a basic variable don't get deleted automatically (garbage collected). Is this true?

Situation 1:
If I do this:

%tBattler = new scriptObject(BattlerClass) { id = 1; }
%tBattler = new scriptObject(BattlerClass) { id = 2; }

is the %tBattler with id=1 deleted? Or must I manually delete it?

Situation 2:

function MakeBattler()
{
%tBattler = new scriptObject(BattlerClass) { id = 1; }
}

Since %tBattler is a local variable, is %tBattler automatically deleted after the function ends? Or must I manually delete it?

Thanks!
#3
08/06/2012 (7:49 am)
Another memory question, regarding loading outside files, as in lines below:

%Filename1 = "data/images/MyPicture1.png";
%Picture1 = expandFilename(%Filename1);
myBitmapCtrl.setBitmap(%Picture1);

%Filename2 = "data/images/MyPicture2.png";
%Picture2 = expandFilename(%Filename2);
myBitmapCtrl.setBitmap(%Picture2);

When myBitmapCtrl loads %Picture2, is %Picture1 removed from memory? Or is it still in memory?
#4
08/06/2012 (7:15 pm)
The annoying bit about preload is that it also overrides allowUnload.
In order to unload images from memory you need preload = "0" and allowUnload = "1".

How the imageDatablocks work is there is a counter for all of the objects that are using the datablock. When the counter hits 0, and allowUnload is true, then the texture is dumped from memory, and has to be reloaded if you create a new object of that type. (*rant* if you have an t2dAnimatedSprite and you are using multiple animations, you would think that putting all the animations on the same spritesheet would keep the counter above 0 when you change animations - *it doesn't*) If preload is true, then when you create the datablock, it sets the counter to 1 when the imageDatablock is created, ensuring that it never gets unloaded.