Forum > General

Cration of TShape causes access violation error

(1/1)

bruce.button:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit main_form; {$mode objfpc}{$H+} interface uses        Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type        TPointArray = array of TShape;         { TForm1 }  TForm1 = class(TForm)                btn_Generate: TButton;                procedure btn_GenerateClick(Sender: TObject);  procedure FormCreate(Sender: TObject);        private         public         end;   var        Form1: TForm1;        points: TPointArray;   implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin end; procedure TForm1.btn_GenerateClick(Sender: TObject);var        i: integer; begin        for i := 1 to 20 do        begin                points[i] := TShape.Create(Application);                points[i].Shape := stCircle;                points[i].left := 60 + 30 * i;                points[i].Top := 200;                points[i].width := 10;                points[i].height := 10;                points[i].Brush.Color := clRed;        end;end; end.  

eljo:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.btn_GenerateClick(Sender: TObject);var        i: integer; begin  SetLength(Points, 20)        for i := 0 to 19 do        begin                points[i] := TShape.Create(Application);                points[i].Shape := stCircle;                points[i].left := 60 + 30 * i;                points[i].Top := 200;                points[i].width := 10;                points[i].height := 10;                points[i].Brush.Color := clRed;        end;end;

bruce.button:
Thank you, Eljo, that solves the problem. Stupid mistake!

Navigation

[0] Message Index

Go to full version