SOLVED : type record access violation lazarus / 62-General ---------------------------------------------------------------------- Jumbo · 2020-02-16 21:26 Using next  code and call te procedure OpenHistory() I get the access violation: ============================================ unit Global; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,   fphttpclient, RegExpr; type   TForm3 = class(TForm)   private     { private declarations }   public     { public declarations }     Procedure OpenHistory();   end; type   SStation = record     SUrl:String;     SImg:String;   end; var   G: TForm3;   History: Array of SStation; implementation {$R *.lfm} Procedure TForm3.OpenHistory(); begin   // History[0] := SStation. how to make room... //    History[0].SUrl:='Url';    History[0].SImg:='Img'; end; --------------------------------------------- When I call this procedure  I get the access violation. I call it as G.OpenHistory() and Global is in the uses.... its about just this procedure. Should I have first done some  new or create for the new record of type History ? The Compiler likes it but on runtime it fails. Any help is appreciated. ( I tried a same thing with TstringList, which sould be possible, but that blows my mind totally , even with the lost of examples.) ---------------------------------------------------------------------- winni · 2020-02-16 21:34 Hi! Procedure TForm3.OpenHistory(); begin   SetLength(HIistory,1);    History[0].SUrl:='Url';    History[0].SImg:='Img'; end; Winni ---------------------------------------------------------------------- Jumbo · 2020-02-17 12:30 Silly I could not find that,  Winni, thanks for the answer! No create, no new ... just increase the number of records. It seems to work, but I have no idea why.... Now the code is: .... begin    Cnt:= Length(History);    Log('Cnt : '+ IntToStr(Cnt));    SetLength(History,Cnt+1);    History[Cnt].SUrl:='Url';  ... ...  Works as a charm... ---------------------------------------------------------------------- Thaddy · 2020-02-17 14:27 In 3.2.0 there is another syntax to achieve this, see:https://wiki.fre epascal.org/FPC_New_Features_3.2#Dynamic_Arrays_have_built_in_.2B_oper ator_support Example:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode delphi}{$H+}type  SStation = record    SUrl:String;    SImg:String;  end; var   History: Array of SStation =[]; // initialized dynamic array  S:SStation;begin  // Fill a record  S.Surl :='http://example.com';  S.Simg:='img';  // And simply add it, does not need setlength  History := History + [S];  // Add another  S.Surl :='http://www.example.com';  S.Simg:='img2';  History := History + [S];  // Show records in History  for S in History do writeln(S.Surl,sLineBreak,S.SImg);end.Just for completeness. ---------------------------------------------------------------------- lucamar · 2020-02-17 14:30 Quote from: Jumbo on February 17, 2020, 01:30:15 pmIt seems to work, but I have no idea why.... When you declare a dynamic array (an array of sometype) variable there is no way for the compiler to guess how it'll be used or how much space to reserve for it, so it just makes a note that such a variable exists and assigns a null pointer for it. If you then try use that variable before "constructing" the array the operation means basically dereferencing a null pointer, which of course causes an access violation. Only when you SetLength to the array this happens (very basically!): some memory is reserved for X items and the var is made to point to that newly allocated block. From then on you can safely use the array items for whatever you want. But take care! If you try to address items beyond the assigned length it will fail again: you must grow (or diminish) your array as needed. Note also that by this same rule, setting the array's length to 0 (zero) will deallocate any memory assigned to it and revert back to the starting point and, IIRC, the "pointer" will again be Nil. HTH