SRL Archive - OSR Intermediate Scripting Tutorials

Debugging memory leaks


Wizzup?

2011-02-09 05:44:50

If you see something like this when you stop your script:

The following DTMs were not freed: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, SRL - Lamp bitmap, 82]
The following bitmaps were not freed: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, SRL - Mod bitmap, SRL - Admin bitmap]
File[C:\Simba\Includes\SRL/logs/SRL log 08-02-11 5.txt] has not been freed in the script, freeing it now.

You probably have a memory leak in your script. (Look at the insane amount of DTMs - it may be possible these are all globally in use, in which case you would not have to debug, but let's assume this is not the case)

Now, how can you debug this? The hard way would be to look at scripts and find all the points where you load a DTM or Bitmap, and then trace all possible routes the variable can go.

There is a slightly more simple method; Simba provides these two functions:

SetDTMName(DTM: Longint; name: string);
SetBitmapName(Bmp: Longint; name: string);

If you give each DTM you load a name; Simba will print it's name instead of the number when the script is terminated. This should (if used correctly) make debugging memory leaks a whole lot easier.

Example:
program new;

var bmp: integer;

begin
bmp:=CreateBitmap(42, 42);
setbitmapname(bmp, 'My 42x42 bitmap that I use to find black shields');
end.

Compiled succesfully in 17 ms.
Successfully executed.
The following bitmaps were not freed: [My 42x42 bitmap that I use to find black shields]

That's all, I just wanted to make you guys aware of this feature.

Flight

2011-02-09 08:41:21

This is exactly what I need to find my leak, but could you think of a way to pinpoint in which line the bitmap/DTM is being 'leaked' and/or not freed?

I'll definitely use this because running my scripts for between 7 and 10 hours I'll come up with 5,000 - 8,000 unfreed bitmaps, and I honestly can't figure out why at this point.

Echo_

2011-02-09 09:04:22

Wow, I didn' t know about those functions! Thanks for this tut.

Coh3n

2011-02-09 13:45:37

I didn't know this either. Although MSI's DTMs are all global, it wouldn't hurt to add this.

Thanks. :)

Sir R. M8gic1an

2011-02-09 15:12:31

hummm. This should definitely be in MSI and SRL, if we know which ones are leaking it should be easy enough to find where they are leaking. Cool tut.

Anyways, I say 'we' out of habbit. I haven't moved a coding finger in ages.

EvilChicken!

2011-02-09 15:35:03

Hey, this was cool – I love to finding out about features like this.
Any more cool stuff you care to share?

Camaro'

2011-02-09 19:48:25

From: EvilChicken!
Hey, this was cool – I love to finding out about features like this.
Any more cool stuff you care to share?


Yea, this is cool. Any more cool stuff you got for us? :)

Nava2

2011-02-09 20:14:14

Freeze() and Unfreeze()

Wizzup?

2011-02-10 04:51:35

From: Nava2
Freeze() and Unfreeze()


Documented here; kind of. http://docs.wizzup.org/simba/scriptref/window.html?highlight=freeze

SetBitmapName: http://docs.wizzup.org/simba/scriptref/bitmaps.html#setbitmapname

I can't think of other features that might be useful at the moment.
If you want to find out stuff about Simba, I really recommend the Simba documentation. It's not fully complete but it does have quite some information in there. Oh, and if you find any mistakes in the documentation or would like some specific item documented (in a better way / at all), let us know.

mastaraymond

2011-02-10 07:39:46

C'mon dudes! This has been around for ages, see SRL ;-)

Sir R. M8gic1an

2011-02-10 08:03:05

So the picture I draw from this is the following… for years we have had scar and we are used to it.

The way we got to know scar was through it's documentation really. The people who knew it best wrote the documentation and then we read it and that's why then people made tuts and then people got to know scar; And we've been used to scar for years and Simba is new.

What Wizzup is saying in a way, is that Simba is new and has new things compared to scar. So everyone should read up on Simba's documentation like we originally did with scar.

I'm guilty of this too! But I haven't scripted in ages. And a thought that just popped into my mind is that Simba is great as it is, and pretty far documented and well spread through the community and so on.

Wizzup, what we need is scripts from you guys :) You guys know Simba so well that you'd be showing us unbelievably cool things even if you just simply wrote a new miner.

Something basic quick in simba, and besides, us devs usually have our own libraries so it can't be too hard to put a script together.

I've got my own libraries too, as do many people, but you (simba devs) the simba library is yours.

Can I borrow a book off of you? Is there any book you'd like to show me?

I'm going to put this out to public.

You guys should release more of your scripts in devs. You guys are great and you know I know and there's many more people who know, but if we (devs) dont get so see any of your recent stuff posted in devs then we can't really look up to you. You're impossibly high.

That's why I created MSI. I wanted to pass my knowledge to someone, but I knew that my scripts in devs would be shameful compared to what you can do.

Now, as you also know, I'm in a process of getting back into scripting, and you know tht because I keep posting my ideas in this thread in devs about what script I'm gonna do.

The script is actually pretty much done cus all I need is to pop together my libraries, give it a quick clean and put it all together.

But I have no one leading me, and I'm a shit scripter, especially shit simba scripter compared to you, so what we actually need is to see good scripts from the big boys.

Sure, people used to be able to write good scripts in scar and our simba scripts are so much less than they could be.. but we have no one's lead here.

Make a script with simba. I dont care all that much about SRL 5, SRL works just fine.

What I dont know is how simba works. So I want to see some simba scripts.

And there are quite a few simba scripts.. but they look pretty scarish to me.

And I'd never understand SRL 5, or I'd never know the possibilities for SRL 5 which is SIMBA made, because I haven't seen any real simba scripts by the pros. We all learnt off of scripts initially, and then the include and then the interpreter.

You need to make a script. And I think Ray probably has a nice script stored somewhere already.

Pass some goods around my friend :) Release it in staff, release it in devs, maybe even release it in SSRL! We all learnt much from the script Markus posted there, but that's only one script by Markus, which is not bad at all, and not only by markus, but I don't think Markus is at your level. And I dont think I'm at Markus' level, but I think I could be if I had scripts from you in front of me to learn from.

And I dunno how much scripting fawk's been doing, but he's got no scripts to look at in the forums either!

What I'll point out one last time in this post is that my knowledge, my scripting knowledge or at least some influences, were passed down in MSI.

MSI is imo a step between members and devs. I created that place, and this is what I've always said about it.

And I could explain that if someone asked nicely. People usually just disagree instead of asking what I mean. Kinda

~RM

Wizzup?

2011-02-10 08:40:48

I have no scripts at the moment. All are pre-Simba. I intend to make a few soon.

mastaraymond

2011-02-10 08:52:13

From: Sir R. M8gic1an
So the picture I draw from this is the following… for years we have had scar and we are used to it.

The way we got to know scar was through it's documentation really. The people who knew it best wrote the documentation and then we read it and that's why then people made tuts and then people got to know scar; And we've been used to scar for years and Simba is new.

What Wizzup is saying in a way, is that Simba is new and has new things compared to scar. So everyone should read up on Simba's documentation like we originally did with scar.

I'm guilty of this too! But I haven't scripted in ages. And a thought that just popped into my mind is that Simba is great as it is, and pretty far documented and well spread through the community and so on.

Wizzup, what we need is scripts from you guys :) You guys know Simba so well that you'd be showing us unbelievably cool things even if you just simply wrote a new miner.

Something basic quick in simba, and besides, us devs usually have our own libraries so it can't be too hard to put a script together.

I've got my own libraries too, as do many people, but you (simba devs) the simba library is yours.

Can I borrow a book off of you? Is there any book you'd like to show me?

I'm going to put this out to public.

You guys should release more of your scripts in devs. You guys are great and you know I know and there's many more people who know, but if we (devs) dont get so see any of your recent stuff posted in devs then we can't really look up to you. You're impossibly high.

That's why I created MSI. I wanted to pass my knowledge to someone, but I knew that my scripts in devs would be shameful compared to what you can do.

Now, as you also know, I'm in a process of getting back into scripting, and you know tht because I keep posting my ideas in this thread in devs about what script I'm gonna do.

The script is actually pretty much done cus all I need is to pop together my libraries, give it a quick clean and put it all together.

But I have no one leading me, and I'm a shit scripter, especially shit simba scripter compared to you, so what we actually need is to see good scripts from the big boys.

Sure, people used to be able to write good scripts in scar and our simba scripts are so much less than they could be.. but we have no one's lead here.

Make a script with simba. I dont care all that much about SRL 5, SRL works just fine.

What I dont know is how simba works. So I want to see some simba scripts.

And there are quite a few simba scripts.. but they look pretty scarish to me.

And I'd never understand SRL 5, or I'd never know the possibilities for SRL 5 which is SIMBA made, because I haven't seen any real simba scripts by the pros. We all learnt off of scripts initially, and then the include and then the interpreter.

You need to make a script. And I think Ray probably has a nice script stored somewhere already.

Pass some goods around my friend :) Release it in staff, release it in devs, maybe even release it in SSRL! We all learnt much from the script Markus posted there, but that's only one script by Markus, which is not bad at all, and not only by markus, but I don't think Markus is at your level. And I dont think I'm at Markus' level, but I think I could be if I had scripts from you in front of me to learn from.

And I dunno how much scripting fawk's been doing, but he's got no scripts to look at in the forums either!

What I'll point out one last time in this post is that my knowledge, my scripting knowledge or at least some influences, were passed down in MSI.

MSI is imo a step between members and devs. I created that place, and this is what I've always said about it.

And I could explain that if someone asked nicely. People usually just disagree instead of asking what I mean. Kinda

~RM

Almost all functions in Simba are created and tested with a test-script..

https://github.com/MerlijnWajer/Simba/tree/master/Tests/PS

Flight

2011-02-11 20:26:49

Wizzup?, I had a quick question on this subject which I'm sure you or anyone else would be able to answer, it's probably a very simple problem for you.

I've used this to debug which bitmaps are not being freed, and although I've looked over the code so many times, I've yet to come up with where they're not correctly being freed. Here's an example:

procedure CheckLogin;
Var x,y:integer;
begin
LoginButton := BitmapFromString(12, 8, 'meJxr+P+/AYwYGhgYZjAwHGMAMhp' +
'gglDxBoQgMpsYNWiCmOJoajC5mHah2DsBn13ICIuaFQzi/dj9C2cD' +
'AOa4mlM=');
setbitmapname(LoginButton, 'LoginButton');
if (FindBitmapToleranceIn(LoginButton,x,y,60,20,390,305,25)) then
begin
blah;
blah2;
meh;
FreeBitmap(LoginButton);
end else
begin
FreeBitmap(LoginButton);
end;
end;
Note: This method is repeated within the main script loop, and for what it's worth this is NOT for Runescape, but I don't see where that would matter.

Wizzup?

2011-02-12 05:15:50

What is blah, blah2 and meh? If there is an exit in it, then the bitmap may still not be freed.

Flight

2011-02-12 05:50:03

So then the bitmap must be freed in the same function/procedure it's called in? That would make sense if the procedure calls another function/procedure before the bitmap is freed, and if this were the case a simple change in order would fix it.

Maybe change this:
begin
blah;
blah2;
meh;
FreeBitmap(LoginButton);
end else

to

begin
FreeBitmap(LoginButton);
blah;
blah2;
meh;
end else

I don't see why that wouldn't correctly free my bitmap.

Wizzup?

2011-02-12 05:56:41

If you free it directly after the findbitmap, and at the `else'; then there should be no leak, yes.

Sir R. M8gic1an

2011-02-12 06:57:00


try
loadBitmap();

finally
FreeBitmap();

~RM

PatDuffy

2012-01-14 18:54:43

Last post ~1 year ago? I don't care, this deserves a bump :p

I never knew about this, just helped me find 3 DTM's that I forgot to free that were building up in the memory.