Recent

Author Topic: why do I get Error: illegal qualifer when trying to free a dynamic string array  (Read 3418 times)

jw

  • Full Member
  • ***
  • Posts: 126
see code

Code: [Select]

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }


     words: array of string;


  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin

    SetLength(words ,5);
    words[0] := 'hello';
    words[1] := 'how';
    words[2] := 'are';
    words[3] := 'you';
    words[4] := 'today';

end;

procedure TForm1.Button2Click(Sender: TObject);
var
   arrayfree : integer;
begin

    for arrayfree:=Low(words) to High(words) do
        words[arrayfree].free;   //errors on this command


end;

initialization
  {$I unit1.lrs}

end.
                                          




should I just use setlength?? this works for dynamically created object and freeing them but I can't free my arrays of strings

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Both dynamic arrays and (ansi)strings are automated sizes, and these two types are closely related.

They will be automatically deallocated once they go out of scope, and can be forced to release by setting their length to 0.

If dyn arrays are of an automated type (like array of ansistring), the string will also be freed.

Probably you don't have to do nothing at all.

 

TinyPortal © 2005-2018