Recent

Author Topic: Cration of TShape causes access violation error  (Read 367 times)

bruce.button

  • Jr. Member
  • **
  • Posts: 59
Cration of TShape causes access violation error
« on: June 06, 2023, 07:40:57 am »
I am trying to create an array of circles (using TShape). However, when I run the code I get an Access violation error on the line (52):

   points[index] := TShape.Create(Application);

The complete code is below.

Thank you for this very helpful forum!


Code: Pascal  [Select][+][-]
  1. unit main_form;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.         Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.         TPointArray = array of TShape;
  12.  
  13.         { TForm1 }
  14.  
  15.  TForm1 = class(TForm)
  16.                 btn_Generate: TButton;
  17.                 procedure btn_GenerateClick(Sender: TObject);
  18.   procedure FormCreate(Sender: TObject);
  19.         private
  20.  
  21.         public
  22.  
  23.         end;
  24.  
  25.  
  26.  
  27. var
  28.         Form1: TForm1;
  29.         points: TPointArray;
  30.  
  31.  
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.FormCreate(Sender: TObject);
  40.  
  41. begin
  42.  
  43. end;
  44.  
  45. procedure TForm1.btn_GenerateClick(Sender: TObject);
  46. var
  47.         i: integer;
  48.  
  49. begin
  50.         for i := 1 to 20 do
  51.         begin
  52.                 points[i] := TShape.Create(Application);
  53.                 points[i].Shape := stCircle;
  54.                 points[i].left := 60 + 30 * i;
  55.                 points[i].Top := 200;
  56.                 points[i].width := 10;
  57.                 points[i].height := 10;
  58.                 points[i].Brush.Color := clRed;
  59.         end;
  60. end;
  61.  
  62. end.
  63.  
  64.  

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Cration of TShape causes access violation error
« Reply #1 on: June 06, 2023, 08:02:59 am »
1) I do not see any setlength call for the array
2) dynamic arrays alway start at position 0 not 1

something along the lines of.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.btn_GenerateClick(Sender: TObject);
  2. var
  3.         i: integer;
  4.  
  5. begin
  6.   SetLength(Points, 20)
  7.         for i := 0 to 19 do
  8.         begin
  9.                 points[i] := TShape.Create(Application);
  10.                 points[i].Shape := stCircle;
  11.                 points[i].left := 60 + 30 * i;
  12.                 points[i].Top := 200;
  13.                 points[i].width := 10;
  14.                 points[i].height := 10;
  15.                 points[i].Brush.Color := clRed;
  16.         end;
  17. end;


bruce.button

  • Jr. Member
  • **
  • Posts: 59
Re: Cration of TShape causes access violation error
« Reply #2 on: June 06, 2023, 08:13:52 am »
Thank you, Eljo, that solves the problem. Stupid mistake!
« Last Edit: June 06, 2023, 10:53:50 pm by bruce.button »

 

TinyPortal © 2005-2018