Hey guys,
I'm sure I'm not the only one who's had their share of access violation errors, especially the ones that Pascal Script decides not to specify exactly what the problem is. Well, I was reminded of what causes this and was asked to create a thread about it so it doesn't happen to anyone else.
When working with custom types, most people use some sort of "get" function that will return the that custom type. As far as I know, this is the only way to create the access violation, but it may also occur on some built-in types such as TPoints. Run this in a new Simba:
Simba Code:
program new;
type
TExample = record
name: string;
value, count: integer;
end;
function getExample(): TExample;
begin
with result do
begin
name := 'example one';
value := 0;
count := 10;
end;
end;
begin
clearDebug();
if (getExample().value <= 0) then
writeln('Y');
end.
You should get an "Access violation" error. The issue is in line 22: you apparently can't call getExample like that. Keep in mind that this only seems to happen when you use it in a comparison.
The solution? Simply use a variable:
Simba Code:
program new;
type
TExample = record
name: string;
value, count: integer;
end;
function getExample(): TExample;
begin
with result do
begin
name := 'example one';
value := 0;
count := 10;
end;
end;
var
exam: TExample;
begin
clearDebug();
exam := getExample();
if (exam.value <= 0) then
writeln('Y');
end.
Hopefully this will avoid some frustration.
Cheers,
Cohen
P.S. This may end up turning into a FAQ thread.