Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - Boolean property is available even when the object has not been created

I was testing today something, and I noticed that you can access a boolean type property of an object even the instance is not created. How this is possible? When trying to modify the boolean property an AV is raised.

unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TTest = class(TObject)
    public
     bBool : Boolean;
  end;
  TForm4 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.FormCreate(Sender: TObject);
var t : TTest;
begin
 if t.bBool then
  ShowMessage('what????');//this message is showed
 t.bbool := false; //AV...
end;

end.
like image 529
RBA Avatar asked Dec 20 '22 06:12

RBA


1 Answers

Local variables of object-reference type, such as your t variable, are not initialized. They contain whatever value happened to exist on the stack or in the associated register when the function was entered. Your t variable is uninitialized.

Evidently, in your tests, the value in t happens to refer to someplace within the address space of your program, but the region of memory is read-only. You're allowed to read it, but not write it. Under other circumstances, the address might not have been in your process's address space, and in that case, even reading the value would have given an access violation.

In still other circumstances, the address might have been both readable and writable, and then you would have been allowed to write whatever value you wanted to that location. Strange things might have happened later in your program because of the data you wrote to that location; that location is probably owned by some other part of your program.

like image 184
Rob Kennedy Avatar answered Dec 24 '22 02:12

Rob Kennedy