Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6879479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:51:07+00:00 2026-05-27T04:51:07+00:00

Can I rely on the fact, that an interface field in a record is

  • 0

Can I rely on the fact, that an interface field in a record is always initialized to nil?

TMyRec = record  
  FGuard : IInterface;
  FObject : TObject;
  procedure CheckCreated;
end;

This would allow me to write:

procedure TMyCheck.CheckCreated;
begin
if (FGuard = nil) then
  begin
  FObject := TObject.Create;
  FGuard := TGuard.Create (FObject);
  end;
end;

(for automatic lifetime management)

I know that interface fields are initialized to nil but is that also true when contained in a record?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:51:08+00:00Added an answer on May 27, 2026 at 4:51 am

    Yes you can rely on that.

    All reference-counted variables:

    • Strings;
    • Dynamic arrays;
    • Variants;
    • Interfaces;
    • Nested records containing those kind of variables.

    are initialized to nil when a record is allocated, if you use New or a dynamic array – even locally on the stack. Of course, if you use a plain GetMem or work with pointers, you’ll have to initialize it by yourself (e.g. using a FillChar).

    If you are curious, there is an hidden call to the following procedure of System.pas:

    procedure _InitializeRecord(p: Pointer; typeInfo: Pointer);
    

    This will fill all the reference-counted variables memory to 0, but won’t set the other members of the record. In fact, in a class instance, the whole field memory is initialized with 0, including all members – for a record, the initialization is only for reference-counted types.

    Note that in some cases, I’ve found out that this initialization was not properly generated if you use the object type instead of record – at least under Delphi 2009-2010. So if your code has some object type declaration, you may better switch to record (and loose inheritance), or explicitly call FillChar.

    If you are curious, here is an optimized version I wrote in asm – available in our enhanced RTL.

    procedure _InitializeRecord(p: Pointer; typeInfo: Pointer);
    // this procedure is called at most object creation -> optimization rocks here!
    asm
            { ->    EAX pointer to record to be initialized }
            {       EDX pointer to type info                }
            MOVZX   ECX,[EDX+1]                  { type name length }
            PUSH    EBX
            PUSH    ESI
            PUSH    EDI
            MOV     EBX,EAX                     // PIC safe. See comment above
            LEA     ESI,[EDX+ECX+2+8]           { address of destructable fields }
            MOV     EDI,[EDX+ECX+2+4]           { number of destructable fields }
    @@loop:
            mov edx,[esi]    // type info
            mov eax,[esi+4]
            mov edx,[edx]
            add esi,8
            add eax,ebx      // data to be initialized
            movzx ecx,[edx]  // data type
            cmp ecx,tkLString
            je @@LString
            jb @@err
            cmp ecx,tkDynArray
            je @@DynArray
            ja @@err
            jmp dword ptr [ecx*4+@@Tab-tkWString*4]
            nop; nop; nop // align @@Tab
    @@Tab:  dd @@WString,@@Variant,@@Array,@@Record
            dd @@Interface,@@err
    @@LString:
    @@WString:
    @@Interface:
    @@DynArray: // zero 4 bytes in EAX
            dec edi
            mov dword ptr [eax],0
            jg @@loop
            POP     EDI
            POP     ESI
            POP     EBX
            RET
    @@Variant: // zero 16 bytes in EAX
            xor ecx,ecx
            dec edi
            mov [eax],ecx
            mov [eax+4],ecx
            mov [eax+8],ecx
            mov [eax+12],ecx
            jg @@loop
            jmp @@exit
    @@err:
            MOV     AL,reInvalidPtr
            POP     EDI
            POP     ESI
            POP     EBX
            JMP     Error
    @@Array:
    @@Record: // rarely called in practice
            mov ecx,1
            call _InitializeArray
            dec edi
            jg @@loop
    @@exit:
            POP     EDI
            POP     ESI
            POP     EBX
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I rely on the fact that the underlying field to a property named
If a Thread creates a daemon Thread, can I rely on the fact that
I know you can't rely on equality between double or decimal type values normally,
Can somebody point me to a resource that explains how to go about having
Can a LINQ enabled app run on a machine that only has the .NET
I'm looking for a control that I can put on top of an already
There is plenty of information out there concerning WCF clients and the fact that
Given some text that occupies about 10 rows, how can I resize its container
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you cast a List<int> to List<string> somehow? I know I could loop through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.