SetLength Access violation
Hi
I have a problem when I want to change the size of a dynamic array but if the data is fill with value other than 0 I have an error message Access Violation
I do a simple unit to reproduce this error
Step by step, the error append when SetLength(VarB,VarSize) is call
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TBPointReal = array of double;
TForm1 = class(TForm)
Button1: TButton;
procedure InitVar;
procedure AddVar(VarSize: integer);
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
VarA,VarB : TBPointReal;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.AddVar(VarSize: integer);
begin
SetLength(VarA,VarSize);
SetLength(VarB,VarSize);
end;
procedure TForm1.InitVar;
begin
FillChar(VarA,SizeOf(VarA),0);
FillChar(VarB,SizeOf(VarB),999);
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
AddVar(4);
InitVar;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
AddVar(8);
end;
end. |
Why this code is not correct
Thankyou