Recent

Author Topic: Assigned() Issues  (Read 5322 times)

speleomania

  • New member
  • *
  • Posts: 8
Assigned() Issues
« on: February 09, 2017, 07:36:33 pm »
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

  • Hero Member
  • *****
  • Posts: 4144
Re: Assigned() Issues
« Reply #1 on: February 09, 2017, 07:48:05 pm »
I can't reproduce your issue. The following displays False, as one would expect:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$Mode delphi}{$H+}
  4.  
  5. var
  6.   obj: TObject;
  7.  
  8. begin
  9.   WriteLn('Value of ''Assigned(obj)'' is ',Assigned(obj));
  10. end.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Assigned() Issues
« Reply #2 on: February 09, 2017, 08:47:58 pm »
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][+][-]
  1. program Project1;
  2.      
  3. {$Mode delphi}{$H+}
  4.      
  5. var
  6.   obj: TObject = nil;    // <--- initialization
  7.      
  8. begin
  9.   WriteLn('Value of ''Assigned(obj)'' is ',Assigned(obj));
  10. end.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Assigned() Issues
« Reply #3 on: February 09, 2017, 09:18:22 pm »
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

  • New member
  • *
  • Posts: 8
Re: Assigned() Issues
« Reply #4 on: February 09, 2017, 10:52:22 pm »
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

  • Newbie
  • Posts: 2
Re: Assigned() Issues
« Reply #5 on: August 18, 2021, 02:15:37 pm »
With this example the error occurs.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TData }
  13.  
  14.   TData = class
  15.   private
  16.     fDateTime : TDateTime;
  17.     fName: String;
  18.   public
  19.     constructor Create;
  20.     procedure Clear;
  21.  
  22.     property DateTime: TDateTime
  23.       read fDateTime write fDateTime;
  24.     property Name     : String  read fName      write fName;
  25.   end;
  26.  
  27.  
  28.   { TForm1 }
  29.  
  30.   TForm1 = class(TForm)
  31.     Button1: TButton;
  32.     procedure Button1Click(Sender: TObject);
  33.   private
  34.  
  35.   public
  36.  
  37.   end;
  38.  
  39. var
  40.   Form1: TForm1;
  41.  
  42. implementation
  43.  
  44. {$R *.lfm}
  45.  
  46. { TForm1 }
  47.  
  48. procedure TForm1.Button1Click(Sender: TObject);
  49. var
  50.   aData: TData;
  51. begin
  52.   if Assigned(aData) then
  53.     ShowMessage(aData.Name) // Access Violation
  54.   else
  55.     ShowMessage('False');
  56. end;
  57.  
  58. { TData }
  59.  
  60. constructor TData.Create;
  61. begin
  62.   Clear;
  63. end;
  64.  
  65. procedure TData.Clear;
  66. begin
  67.   fDateTime := Now;
  68.   fName     := '';
  69. end;
  70.  
  71. end.
  72.  

korba812

  • Sr. Member
  • ****
  • Posts: 394
Re: Assigned() Issues
« Reply #6 on: August 18, 2021, 02:21:33 pm »
Local variables are not automatically initialized and may contain random values.
https://www.freepascal.org/docs-html/ref/refse24.html

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Assigned() Issues
« Reply #7 on: August 18, 2021, 02:24:19 pm »
The error occurs because the local variable "aData" is not initialized, i.e. is not nil. Simply declare it like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   aData: TData = nil;
  4. begin
  5.   if Assigned(aData) then
  6.     ShowMessage(aData.Name) // Access Violation
  7.   else
  8.     ShowMessage('False');
  9. end;
  10.  

 

TinyPortal © 2005-2018