Assigned() Issues lazarus / 62-General ---------------------------------------------------------------------- speleomania · 2017-02-09 18:36 Can anyone confirm/comment on this? Program AccessViolation; var   c: TSomeClass; begin   if Assigned(c) then ShowMessage(c.name); // Access violation end; The problem I'm having is that for some reason Assigned seems to always return True unless I manually do begin   c := nil;   if Assigned(c) then ShowMessage(c.name); // This works end; I wasn't expecting having to manually initialise my object vars...is this normal? ---------------------------------------------------------------------- howardpc · 2017-02-09 18:48 I can't reproduce your issue. The following displays False, as one would expect: 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 program Project1; {$Mode delphi}{$H+} var  obj: TObject; begin  WriteLn('Value of ''Assigned(obj)'' is ',Assigned(obj));end. ---------------------------------------------------------------------- wp · 2017-02-09 19:47 Some for me. But I would never rely on a variable being initialized automatically. Even if there seem to be rules (https://stackoverflow.com/questions/132725/are-delphi-variables- initialized-with-a-value-by-default), but I usually forget them (except that class members are always set to zero). Instead, it is easier to remember this more general rule: 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 program Project1;     {$Mode delphi}{$H+}     var  obj: TObject = nil;    // <--- initialization     begin  WriteLn('Value of ''Assigned(obj)'' is ',Assigned(obj));end. ---------------------------------------------------------------------- howardpc · 2017-02-09 20:18 Surely you can rely on documented compiler initializations, such as all globals being set to zero/nil and all class fields/variables set likewise; and strings and dynamic arrays always having an initial zero length? Not that it hurts to make the initial value explicit, as in your example (however, perhaps in some cases, the nil value is then actually assigned twice). ---------------------------------------------------------------------- speleomania · 2017-02-09 21:52 My bad! I'm ashamed to admit I completely forgot about a class variable with the same name which messed me up. You guys are correct and free pascal is not at fault here Case closed! ---------------------------------------------------------------------- anfm · 2021-08-18 12:15 With this example the error occurs. 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TData }   TData = class  private    fDateTime : TDateTime;    fName: String;  public    constructor Create;    procedure Clear;     property DateTime: TDateTime      read fDateTime write fDateTime;    property Name     : String  read fName      write fName;  end;    { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  aData: TData;begin  if Assigned(aData) then    ShowMessage(aData.Name) // Access Violation  else    ShowMessage('False');end; { TData } constructor TData.Create;begin  Clear;end; procedure TData.Clear;begin  fDateTime := Now;  fName     := '';end; end. ---------------------------------------------------------------------- korba812 · 2021-08-18 12:21 Local variables are not automatically initialized and may contain random values. https://www.freepascal.org/docs-html/ref/refse24.html ---------------------------------------------------------------------- wp · 2021-08-18 12:24 The error occurs because the local variable "aData" is not initialized, i.e. is not nil. Simply declare it like this: 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 procedure TForm1.Button1Click(Sender: TObject);var  aData: TData = nil;begin  if Assigned(aData) then    ShowMessage(aData.Name) // Access Violation  else    ShowMessage('False');end;