Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Unexplainable EAccessViolation

UGuardianUGuardian Member Posts: 27
[b][red]This message was edited by UGuardian at 2005-4-3 18:33:44[/red][/b][hr]
The program I'm working on at the moment always trips a runtime error.

When run via Delphi's run menu, the error is an access violation (and naturally the code is shown).

The weird part is, this exact code compiled correctly and ran without any errors in another program I did a few weeks ago. I cut & pasted the code, so a typo is very unlikely.

Here's the class that's causing the problem:
[code]

TOrb = class(TObject)
private
X, Y : Integer;
public
constructor Create;
function IsHere(M,N : Integer) : boolean;
end;


constructor TOrb.Create;
begin
inherited Create;
X := Random(20);
Y := Random(20);
end;

function TOrb.IsHere(M,N : Integer) : boolean;
begin
if X = M then
if Y = N then
IsHere := True else IsHere := False;
end;
[/code]

Whenever X or Y is accessed (read or writen) it trips the error. Even in the TOrb.Create code.

What could be causing this weird behavior?


-edit-

After comparing the source of this program and the one I did a while back I spotted an error in the code. It was not in the TOrb class -- it was in the Form's Create method.

Probably moving too fast I'd incorrectly created TOrbs, and since it was a null refence it crashed like it was supposed to.

I just wish Delphi was more accurate as to which line of code the error's on. The error was no where near where it said it was.

Comments

  • zibadianzibadian Member Posts: 6,349
    : [b][red]This message was edited by UGuardian at 2005-4-3 18:33:44[/red][/b][hr]
    : The program I'm working on at the moment always trips a runtime error.
    :
    : When run via Delphi's run menu, the error is an access violation (and naturally the code is shown).
    :
    : The weird part is, this exact code compiled correctly and ran without any errors in another program I did a few weeks ago. I cut & pasted the code, so a typo is very unlikely.
    :
    : Here's the class that's causing the problem:
    : [code]
    :
    : TOrb = class(TObject)
    : private
    : X, Y : Integer;
    : public
    : constructor Create;
    : function IsHere(M,N : Integer) : boolean;
    : end;
    :
    :
    : constructor TOrb.Create;
    : begin
    : inherited Create;
    : X := Random(20);
    : Y := Random(20);
    : end;
    :
    : function TOrb.IsHere(M,N : Integer) : boolean;
    : begin
    : if X = M then
    : if Y = N then
    : IsHere := True else IsHere := False;
    : end;
    : [/code]
    :
    : Whenever X or Y is accessed (read or writen) it trips the error. Even in the TOrb.Create code.
    :
    : What could be causing this weird behavior?
    :
    :
    : -edit-
    :
    : After comparing the source of this program and the one I did a while back I spotted an error in the code. It was not in the TOrb class -- it was in the Form's Create method.
    :
    : Probably moving too fast I'd incorrectly created TOrbs, and since it was a null refence it crashed like it was supposed to.
    :
    : I just wish Delphi was more accurate as to which line of code the error's on. The error was no where near where it said it was.
    :
    :
    I see nothing wrong with this class which could raise an EAcessViolation. Perhaps you called the IsHere() method without creating an instance of the object.
    On a side note: normally you return function result values using the implicitly defined "Result"-variable. Assigning the function name to a value is still supported for backwards compatibility.
  • KoppisKoppis Member Posts: 68
    : : [b][red]This message was edited by UGuardian at 2005-4-3 18:33:44[/red][/b][hr]
    : : The program I'm working on at the moment always trips a runtime error.
    : :
    : : When run via Delphi's run menu, the error is an access violation (and naturally the code is shown).
    : :
    : : The weird part is, this exact code compiled correctly and ran without any errors in another program I did a few weeks ago. I cut & pasted the code, so a typo is very unlikely.
    : :
    : : Here's the class that's causing the problem:
    : : [code]
    : :
    : : TOrb = class(TObject)
    : : private
    : : X, Y : Integer;
    : : public
    : : constructor Create;
    : : function IsHere(M,N : Integer) : boolean;
    : : end;
    : :
    : :
    : : constructor TOrb.Create;
    : : begin
    : : inherited Create;
    : : X := Random(20);
    : : Y := Random(20);
    : : end;
    : :
    : : function TOrb.IsHere(M,N : Integer) : boolean;
    : : begin
    : : if X = M then
    : : if Y = N then
    : : IsHere := True else IsHere := False;
    : : end;
    : : [/code]
    : :
    : : Whenever X or Y is accessed (read or writen) it trips the error. Even in the TOrb.Create code.
    : :
    : : What could be causing this weird behavior?
    : :
    : :
    : : -edit-
    : :
    : : After comparing the source of this program and the one I did a while back I spotted an error in the code. It was not in the TOrb class -- it was in the Form's Create method.
    : :
    : : Probably moving too fast I'd incorrectly created TOrbs, and since it was a null refence it crashed like it was supposed to.
    : :
    : : I just wish Delphi was more accurate as to which line of code the error's on. The error was no where near where it said it was.
    : :
    : :
    : I see nothing wrong with this class which could raise an EAcessViolation. Perhaps you called the IsHere() method without creating an instance of the object.
    : On a side note: normally you return function result values using the implicitly defined "Result"-variable. Assigning the function name to a value is still supported for backwards compatibility.

    Could you show how you are using this class? Probably the problem is there.
    By the way, doesn't Delphi show you any warnings at function Ishere? You don't assign any value there, if X<>M. That is not nice at all.
    How about this:[code]
    function TOrb.IsHere(M,N : Integer) : boolean;
    begin
    Result := (X=M) and (Y=N);
    end;[/code]
Sign In or Register to comment.