SRL Archive - Help and Support

HOW 2.0: Fixing Access Violations.


R0b0t1

2008-01-06 16:57:28

I couldn't decide between the Tutorial Section and this Section. I picked this one because it will be viewed.

Example:

[Runtime Error] : Exception: Access violation at address 00721A4C in module 'scar.exe'. Read of address 00000030 in line 222 in script C:\Documents and Settings\Desktop\Scripts\My Scripts\Nature Runecrafter\NatureCrafter 0.2.2.scar


Well, Fr0zN_S0uL asked me for some help before, and then JackLKrawl made a thread about an error. Which he said was an Access Violation.

I'm tired of typing out Access Violation. I'm going to call it an AV. Mmkay?


So, the first response you may have is bewilderment. Your script is syntacticly correct, you've made sure your countless bitmaps and/or DTM's are correctly initialized, and you've tested every single procedure countlessly.

Wait a second. Notice those bolded words? Thats part of the problem.

Before I tell you leechers how to fix it, lets see how this problem starts. An AV occurs when a program tries to access a segment of memory outside of its alloted space. Why would Delphi try to do this? Well, its because you've given managing memory over to the computer. If you've ever used an unmanaged language such as C, then you know if you use a function such as malloc or calloc to get memory, if it fails it returns a null pointer. Now, as far as I know, Delphi doesn't do this and when it wants memory it will get memory. Now, if a particular segment of memory is outside of the programs bounds, it says: "Too bad! I want it I want it I want it!!!". But Big Daddy Windows steps in and says: "ROFLMAO!!! ACCESS VIOLATION NUBLET!". Why should windows be such a party-pooper? Well, since (unlike the creator of Delphi wanted, but thats another thread) the threshold between data and instructions ain't that clear, you could potentially be overwriting other programs and operating system instructions… And we all know Big Daddy Windows comes first. ;)

So, if you haven't gathered the particular root of this problem yet, its because you're using to much memory, you greedy person you! (Okay, sorry if that sounded weird, as it did to me)

How can you fix this? Well, there are two types of AV's, the ones that use seemingly random locations (The long hexadecimal numbers, FYI), and those that use either all F's or 0's as at least one of the locations. Now, I'm not sure if these really occur in SCAR much, or at all, (I think I may have gotten one once) but this happens when you try to access something you have already freed. This only occurs in scripts that have Forms, Bitmaps, and DTM's in some way or form (no pun intended).

So? You may ask: How is this happening? Well, your code may make sense to you, but you may actually be freeing something before you're last call (As if you accidentally include your "FreeObjects" function in your main loop, something like that) . A good way to fix this would be to only free these things in a procedure called "ScriptTerminate" (fix me if I'm wrong) that SCAR calls when it terminates the script. (It does this by simply declaring an overloadable function… I think. Someone find out how this is done?) Simply include all of you're free calls in there…


Now if you get one with random locations (the hex numbers, remember?), you are simply using to much memory… Problem is, AV's are runtime errors, and you don't know you are using to much memory until it happens. If you've noticed that it usually occurs on lines that include calls to FindColor, the FindObj series, FindDTM, FindBitmap, etc, if because the way SCAR was coded it creates a TCanvas, which, guess what, takes memory! So, is there a way you can fix this? Sure, you can. I've noticed that the most memory is taken up by forms and the pictures people like to put on them, so make sure you free them before you even start the main part of the script. This can be done like:


FreeBitmap(BitmapName);

Be sure you do that for every picture. Now, this is how you free forms (I think I got it wrong..?):


FreeForm(FormName);

How you free a TCanvas (I've noticed people like to use canvases for copying pictures to forms) Also, If you use this before you are done with it you will get the above AV type error (the one with all 0's or F's):


CanvasName.Free;

And how you free DTM's:


FreeDTM(DTMName);

Now, you might only free a DTM at the end of the script… And you still might be getting an AV when you free everything else! Well, what you can do, is if you only use a particular function, create and free it in that function. Thats about the only thing you can hope for :(


I'm sure I've covered all I can here, but if you want personal help PM me.

FrÕzÑ_§ÕµL

2008-01-09 03:15:37

Aha nice tut… a ton based around me I bet :p

From: R0b0t1
I've noticed people like to use canvases for copying pictures to forms

Yea, such as people reading my tut hehe! Thanks anyway, I'm gonna try the Canvas.FreeCanvas. Was getting errors last time but I think I had it in a bad place ;)

R0b0t1

2008-01-11 19:32:28

Meh, just general information :)

But you did play a part in it… And you started that Canvas thing?

Jackrawl

2008-01-11 20:10:12

[Joking] Gud tut [/Joking]

So what about if you're running FreeRAM XP Pro? Would that get in SCAR's personal space?

R0b0t1

2008-01-11 21:56:09

Yes, what does the program say about itself? If it says something about freeing all memory… I dunno, just try to crash your computer. Or Link me to it.