Recent

Author Topic: Initial value of a property in a component  (Read 18356 times)

cdbc

  • Hero Member
  • *****
  • Posts: 2683
    • http://www.cdbc.dk
Re: Initial value of a property in a component
« Reply #15 on: March 11, 2011, 09:01:26 pm »
Hi.

Every Tobject created will have 0, nil null, values assigned to members on creation. (mind you, i might be wrong). Somewhere along, there is a call to fillchar(*,#0) or so or zeromemory(*,*).

Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

eny

  • Hero Member
  • *****
  • Posts: 1658
Re: Initial value of a property in a component
« Reply #16 on: March 11, 2011, 09:56:55 pm »
Every Tobject created will have 0, nil null, values assigned to members on creation.

Hm, I wonder what the 4th line would do...
Code: Pascal  [Select][+][-]
  1. constructor myCustomControl.Create(AOwner: TComponent);  
  2. begin  
  3.    inherited Create(AOwner);  
  4.    FAlpha := 255;      
  5. end;
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

cdbc

  • Hero Member
  • *****
  • Posts: 2683
    • http://www.cdbc.dk
Re: Initial value of a property in a component
« Reply #17 on: March 12, 2011, 04:10:46 am »
Hi

Sorry, my point was...:

constructor myCustomControl.Create(AOwner: TComponent);    
begin    
   inherited Create(AOwner); <- in here some zero-memory is going on at creation time.
   FAlpha := 255;        
end;  

[edit]: My bad...
Code: [Select]
      class function TObject.NewInstance : tobject;

        var
           p : pointer;

        begin
           getmem(p, InstanceSize);
           if p <> nil then
              InitInstance(p);
           NewInstance:=TObject(p);
        end;

      class function TObject.InitInstance(instance : pointer) : tobject; {$ifdef SYSTEMINLINE} inline; {$ENDIF}

        begin
           { the size is saved at offset 0 }
           fillchar(instance^, InstanceSize, 0);
           { insert VMT pointer into the new created memory area }
           { (in class methods self contains the VMT!)           }
           ppointer(instance)^:=pointer(self);
           if PVmt(self)^.vIntfTable <> @emptyintf then
             InitInterfacePointers(self,instance);
           InitInstance:=TObject(Instance);
        end;

No zeroing going on at all...
Seems a bit different from delphi...?

Regards Benny
« Last Edit: March 12, 2011, 04:41:42 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Initial value of a property in a component
« Reply #18 on: March 12, 2011, 10:44:41 am »
fillchar(instance^, InstanceSize, 0); ;)

eny

  • Hero Member
  • *****
  • Posts: 1658
Re: Initial value of a property in a component
« Reply #19 on: March 12, 2011, 11:10:54 am »
No zeroing going on at all...
Seems a bit different from delphi...?
Yep, you're right here, that was one of the Delphi caveats: don't initialize variables before having called the inherited constructor.
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2674
Re: Initial value of a property in a component
« Reply #20 on: March 14, 2011, 12:18:20 pm »
No zeroing going on at all...
Seems a bit different from delphi...?
Yep, you're right here, that was one of the Delphi caveats: don't initialize variables before having called the inherited constructor.

??? as laksen wrote, all classes are initialized to zero (opposite to objects)
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

eny

  • Hero Member
  • *****
  • Posts: 1658
Re: Initial value of a property in a component
« Reply #21 on: March 14, 2011, 01:06:12 pm »
??? as laksen wrote, all classes are initialized to zero (opposite to objects)
I know... and that's a good thing  :D

I seem to recall that in earlier version of Delphi (version 3, many years ago 8)) the values of properties were destroyed as soon as an inherited costructor was called, after you had given them a value in the Create constructor. So the statements:
Code: Pascal  [Select][+][-]
  1. constructor myCustomControl.Create(AOwner: TComponent);    
  2. begin    
  3.   FAlpha := 42;        
  4.   inherited Create(AOwner);    
  5. end;
would still give FAlpha a value of 0.
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

cdbc

  • Hero Member
  • *****
  • Posts: 2683
    • http://www.cdbc.dk
Re: Initial value of a property in a component
« Reply #22 on: March 14, 2011, 11:39:24 pm »
Hi

@Laksen: Overlooked that one  :) May have been a wee bit sleepy...

Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

 

TinyPortal © 2005-2018