Daniel
2012-04-23 03:06:48
Don't Free Your DTMs/Bitmaps!
Time and time again, I get (or see) people complaining about an error in their scripts relating to the title. Most of the time, it's due to the scripter having FreeDTM or FreeBitmap in their scripts. So, DON'T put them in your scripts! Sure it may be good practice too, especially if you plan to move away from the safety of the Simba development environment, but when in your script, especially if you're a beginner, DON'T free them! Yes, any of them!
No, it won't cause memory leaks. Not at least if you load them properly! What I mean by this is DON'T constantly call DTMFromString or BitmapFromString in your scripts. Only call them once, and make sure they're only being called once!
Good:
procedure LoadStuff;
begin
DTM1 := DTMFromString('340394u23hf3o23h892y389yh32h327c23cr23rg23c\==');
DTM2 := DTMFromStrign('3klfnsjht8o43to8y438thu43hf7syiouoy2492i3uyf27q\==');
Bitmap1 := BitmapFromString('q34837r2o3ijfhfiweuyirh2iygirh23grg23i6rgurbui43gi8giuwefhxguwegrugwurguwgbu\==');
Bitmap2 := BitmapFromString('324y3urhfshfiywuichfchfgcyi1y4t718749QRASULUFHGIAWEU6ACRYIQUY\==');
end;
procedure FindStuff;
begin
if(FindBitmap(Bitmap1, x, y)) then
…
if(FindDTM(DTM1, x, y...)) then
…
end;
begin
LoadStuff;
repeat
FindStuff;
Until(AllPlayersInactive);
end.Bad #1:
begin
repeat
LoadStuff; // It's in a loop! NEVER do this!
FindStuff;
Until(AllPlayersInactive);
end.Bad #2:
procedure FindStuff;
begin
// You're loading a bitmap here!
Bitmap1 := BitmapFromString('q34837r2o3ijfhfiweuyirh2iygirh23grg23i6rgurbui43gi8giuwefhxguwegrugwurguwgbu\==');
if(FindBitmap(Bitmap1, x, y)) then
…
// You're loading a DTM here!
DTM1 := DTMFromString('340394u23hf3o23h892y389yh32h327c23cr23rg23c\==');
if(FindDTM(DTM1, x, y...)) then
…
end;
begin
repeat
FindStuff;
Until(AllPlayersInactive);
end.Notice that in the “good” example, that I am only loading things once. I am doing it at the very beginning of the script, in a separate procedure. I have not put it inside a loop, and notice that I'm also not free'ing anything either.
So, what's the big deal?
[INDENT]It will save a lot of time and effort for you (and others if you ask them for help) in fixing/maintaining your scripts. Memory management can be a hassle, trust me, and getting people who don't know how to properly manage what they load is even more of a hassle. So, it's better to not try to manage memory in your scripts, that way you won't get any problems/errors relating to this subject (DTMs/Bitmaps) when you go to run your script.[/INDENT]
But won't it cause memory leaks and stuff?
[INDENT]A memory leak is when something is loaded, but not being freed. If you load [the same] things constantly (i.e. more than once), your memory in Simba will undoubtedly grow minorly (mostly minorly). That is why you should load them only once! And then leave it at that and do nothing else to it (except using it to find things)!
Simba automatically unloads (free's) ALL left over loaded DTMs/Bitmaps in your scripts. It's better to let Simba do it, than for you to do it :)[/INDENT]
REMEMBER to:
- Have every Bitmap AND DTM you want to load in one procedure.
- Call that procedure AT THE BEGINNING of your script in the begin end clause.
- DON'T call it repeatedly, and,
- DON'T Free your DTMs OR Bitmaps!
Good day :)