Thread: How to Free DTM/Bitmap's the correct way

1. Originally Posted by Pakyakkistan
This may be considered grave digging, but seeing as its a tutorial and is meant to be a learning reference; you should probably mention that you need to declare those DTMs as global variables as well. If you don't, and try declaring them as variables in each procedure you search for them, you won't ever find the DTMs because it will try to declare them as 'new' variables in each procedure. Other then that, wonderful guide! Glad it was here so I ensured I was doing it right.

Edit: Also, if you are going to continue calling for the DTMs, you'll need to Load and Free them within the 'main' loop. Or else you'll free them and gt a run-time error because you won't have any DTMs loaded.
Thats more of a subject for DTM tutorial, rather then how to free resources properly. But yes, each of your point is valid.

2. SRL Junior Member
Join Date
Jan 2012
Location
Long Island, NY
Posts
413
Mentioned
5 Post(s)
Quoted
95 Post(s)
Originally Posted by Pakyakkistan
Edit: Also, if you are going to continue calling for the DTMs, you'll need to Load and Free them within the 'main' loop. Or else you'll free them and gt a run-time error because you won't have any DTMs loaded.

3. SRL Junior Member
Join Date
Aug 2012
Location
The Dark Tower
Posts
154
Mentioned
5 Post(s)
Quoted
56 Post(s)
Originally Posted by Zeta Matt
Called DTM's stay on your computer's memory. If many DTM's are piled in it, it will suffer of memory leaking, basicaly... That's why you have to free them.
Originally Posted by Brotein
Assumed that not calling them would cause a stack of DTMs. I know that doing this without globally doing it would cause this. Each time you load that DTM it would add up.

Edit: For example, my Iron Knife smither, remove some of the FreeDTM procedures and watch the DTM amounts stack when you stop the script. But, like I said, I have no idea if its the same thing when you globally declare them

4. SRL Junior Member
Join Date
Jan 2012
Location
Long Island, NY
Posts
413
Mentioned
5 Post(s)
Quoted
95 Post(s)
Originally Posted by Pakyakkistan
Assumed that not calling them would cause a stack of DTMs. I know that doing this without globally doing it would cause this. Each time you load that DTM it would add up.

Edit: For example, my Iron Knife smither, remove some of the FreeDTM procedures and watch the DTM amounts stack when you stop the script. But, like I said, I have no idea if its the same thing when you globally declare them
Why wouldn't you just declare them globally and only load them once?
Simba Code:
program new;var  DTM1, DTM2, DTM3 : Integer;procedure setDTMs;begin  DTM1 := DTMFromString('');  DTM2 := DTMFromString('');  DTM3 := DTMFromString('');end;procedure killDTMs;begin  freeDTMs([DTM1, DTM2, DTM3]);end;begin  setDTMs;  addOnTerminate('killDTMs');  repeat    //do our stuff  until (not isLoggedIn);  killDTMs;  terminateScript;end.

I can use those DTMs in any function or procedure and I only need to load them once, and free them once.

5. Originally Posted by Brotein
Why wouldn't you just declare them globally and only load them once?
Simba Code:
program new;var  DTM1, DTM2, DTM3 : Integer;procedure setDTMs;begin  DTM1 := DTMFromString('');  DTM2 := DTMFromString('');  DTM3 := DTMFromString('');end;procedure killDTMs;begin  freeDTMs([DTM1, DTM2, DTM3]);end;begin  setDTMs;  addOnTerminate('killDTMs');  repeat    //do our stuff  until (not isLoggedIn);  killDTMs;  terminateScript;end.

I can use those DTMs in any function or procedure and I only need to load them once, and free them once.
thats how i do it and thats how i learned in a dtm tut, never had a script bog down or cpu shoot up

6. SRL Junior Member
Join Date
Aug 2012
Location
The Dark Tower
Posts
154
Mentioned
5 Post(s)
Quoted
56 Post(s)
Originally Posted by Brotein
Why wouldn't you just declare them globally and only load them once?
Simba Code:
program new;var  DTM1, DTM2, DTM3 : Integer;procedure setDTMs;begin  DTM1 := DTMFromString('');  DTM2 := DTMFromString('');  DTM3 := DTMFromString('');end;procedure killDTMs;begin  freeDTMs([DTM1, DTM2, DTM3]);end;begin  setDTMs;  addOnTerminate('killDTMs');  repeat    //do our stuff  until (not isLoggedIn);  killDTMs;  terminateScript;end.

I can use those DTMs in any function or procedure and I only need to load them once, and free them once.
Makes sense. Didn't know, never globally declared or free'd my DTMs. This is the only global DTM tut i've looked at, thanks everyone for the pointers.

7. Originally Posted by Sir Ducksworthy
Which will unfortunately leave you with Non Freed Dtm/Bitmap's
This causes excess memory usage on your computer and ultimately slows down Scripts/Simba.[/I][/B]
All DTMs are freed when the script crashed or is stopped.

8. SRL Junior Member
Join Date
Aug 2012
Location
The Dark Tower
Posts
154
Mentioned
5 Post(s)
Quoted
56 Post(s)
Originally Posted by masterBB
All DTMs are freed when the script crashed or is stopped.
Yea, I wasn't paying attention either. Since you have AddOnTerminate there truly isn't a need to free your DTMs in the loop repeatedly. As long as you have them freed on termination; my bad @Brotein; - Thanks tho

Was thinking that they are filling memory as long as they are being 'used'.

9. Originally Posted by Pakyakkistan
Yea, I wasn't paying attention either. Since you have AddOnTerminate there truly isn't a need to free your DTMs in the loop repeatedly. As long as you have them freed on termination; my bad @Brotein; - Thanks tho

Was thinking that they are filling memory as long as they are being 'used'.
Even without the add on terminate, simba will always free them. Filling your memory can only happen while the script is running.

10. SRL Junior Member
Join Date
Aug 2012
Location
The Dark Tower
Posts
154
Mentioned
5 Post(s)
Quoted
56 Post(s)
Originally Posted by masterBB
Even without the add on terminate, simba will always free them. Filling your memory can only happen while the script is running.
Wait what?

You're saying that if I stop the script midway it should free my DTMs? I thought this was why you added it on terminate because it wouldn't free them?

Or are you saying that they are freed once the script is ended? Sorry for my confusion, just wanting to make sure I understand correctly.

11. Originally Posted by Pakyakkistan
Wait what?

You're saying that if I stop the script midway it should free my DTMs? I thought this was why you added it on terminate because it wouldn't free them?

Or are you saying that they are freed once the script is ended? Sorry for my confusion, just wanting to make sure I understand correctly.
Let me explain you a bit how simba handles bitmaps and dtms. When you create a DTM or Bitmap(resource from this moment on) with most of the common used functions, you will get an integer instead of a TMufasaBitmap or TDTM. This integer is not a pointer, nor is it the resource itself(obviously ). Simba manages a list(array) with all the DTMs and a list with all the bitmaps. The integer you get returned is simply the index/place in the array where the resource is stored.

When the script threads end either by being stopped or crashing, simba will free all the bitmaps and dtms it still has in those lists. So it will not cause memory leaks. When this happens simba will display a line you are probably familiar with. ("DTMs not freed..")

So when does freeing actually matter? Assume the following code is used:

Simba Code:
repeat  wait(50);  bankersDTM := DTMFromString('avraagaregarega');until(FindDTM(bankersDTM )); //arrived at bankFreeDTM(bankersDTM);

If the script continues to run this code the memory will be filled with the same DTM of the bankers... Only the last one is freed. I actually see this happen every now and then.
Last edited by masterBB; 03-21-2014 at 05:36 PM.