Recent

Author Topic: Sigsev error. : Bouncing balls  (Read 4424 times)

Zath

  • Sr. Member
  • ****
  • Posts: 354
Sigsev error. : Bouncing balls
« on: July 25, 2016, 06:27:04 pm »
I have copied the code below from a tutorial.
It should allow 5 balls to bounce around on the screen within the form.
It works on the video but I get this error. (See attachment)
It compiles ok yet fails to run.
I did try and remake it from the tutorial code but have obviously got it wrong somewhere.
What have I missed ?

Thanks

Using Laz 1.6 FPC ver 3 x86-64-Win64-win32/win64

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Shape1: TShape;
    Shape2: TShape;
    Shape3: TShape;
    Shape4: TShape;
    Shape5: TShape;
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  Balls: Array [1..5] of TShape;
  Right: Array [1..5] of Boolean;
  Up: Array [1..5] of Boolean;
  x: Integer;
implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  for x := 1 to 5 Do
  Begin
  if Up[x] = True then Balls[x].Top:=Balls[x].Top-5
  else Balls[x].Top:=Balls[x].Top+5;

  if Right[x] = True then Balls[x].Left:=Balls[x].Left+5
  else Balls[x].Left:=Balls[x].Left-5;

  if Balls[x].Top <= 0 then Up[x] := False;
  if Balls[x].Top + Balls[x].Height >= Form1.Height then Up[x] := True;

  if Balls[x].Left <= 0 then Right[x] := True;
  if Balls[x].Left + Balls[x].Width >= Form1.Width then Right[x] :=False;
  end;
end;

end.
« Last Edit: July 25, 2016, 06:48:22 pm by Zath »

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Sigsev error. : Bouncing balls
« Reply #1 on: July 25, 2016, 06:45:08 pm »
Well, that is not so strange as Balls[], Right[], and up[] array's are not initialized

circular

  • Hero Member
  • *****
  • Posts: 3451
    • Personal webpage
Re: Sigsev error. : Bouncing balls
« Reply #2 on: July 25, 2016, 07:15:05 pm »
You need to assign the Shape1, Shape2, etc. to Balls[1], Balls[2], etc.
Conscience is the debugger of the mind

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Sigsev error. : Bouncing balls
« Reply #3 on: July 25, 2016, 07:30:14 pm »
Still, i find it undesirable (actually bad tutorial practice) to leave the other array values undetermined on initialization.

I found out where the tutor originates from. Seem to be schoolfreeware's freepascal/lazarus application tutorials. Its part 13 and the sources in that archive shows what is missing.

circular

  • Hero Member
  • *****
  • Posts: 3451
    • Personal webpage
Re: Sigsev error. : Bouncing balls
« Reply #4 on: July 25, 2016, 07:33:45 pm »
I agree.
Conscience is the debugger of the mind

Zath

  • Sr. Member
  • ****
  • Posts: 354
Re: Sigsev error. : Bouncing balls
« Reply #5 on: July 25, 2016, 08:45:41 pm »
Yes it is that tutorial.
Its a few years old now and I just wanted to use a timer and move objects.

The original tutorial uses just one ball and I forgot I needed to allow for the extra balls I added.

Pity Lazarus just throws up a sigsev error and doesn't suggest what the error is.
Noobs like me need all the help we can get.

I had originally declared the arrays like this, thought it was the error so changed it.
Right, Up: Array [1..5] of Boolean

Thanks for replying.

circular

  • Hero Member
  • *****
  • Posts: 3451
    • Personal webpage
Re: Sigsev error. : Bouncing balls
« Reply #6 on: July 25, 2016, 09:18:41 pm »
You're welcome.

In principle SIGSEGV means that a pointer is not properly defined. A class like TShape is in fact a pointer that specifies where the information about the shape is. As the values of Balls[] are not defined, they are probably zero and the program is not supposed to access.

SIGSEGV means Signal of Segmentation Violation: literally the program accesses something outside of its allowed segment of memory.

It is true that it would be helpful to have some explanation on the message box. I don't know if it is difficult or not to do technically.

To put it into perspective, though, before those mechanism of memory were introduced at the level of the processor of the computer, accessing a random location in memory like would crash the computer. So you would not have any explanation at all and you could lose your unsaved changes.

So in a way, SIGSEGV error is not that bad.
Conscience is the debugger of the mind

Zath

  • Sr. Member
  • ****
  • Posts: 354
Re: Sigsev error. : Bouncing balls
« Reply #7 on: July 25, 2016, 09:46:15 pm »
Oh agreed. Notification of an error, however unhelpful, is better than none at all.

Thanks for the info on sigsev. Very interesting.
 

My next task is to change the ball to an image and/or allow clicking on the object as if it were a target.

Should be fun.

 

TinyPortal © 2005-2018