What should the TIniFiles initial state be after defining in a procedure/function? Shouldn't the value assigned to a Tinifile be nil at the start?

What should the TIniFiles initial state be after defining in a procedure/function? Shouldn't the value assigned to a Tinifile be nil at the start?

When calling this example passing FALSE, the check for not assigned comes up true, yet it isn't. Now if I pass TRUE and manually initialize value to Nil, I'm ok.

This is Delphi 10 Seattle Update 1.

procedure IniWrite(NilFirst: boolean);
var
Ini1: tinifile;
begin
if NilFirst then
Ini1:= nil;

if not(assigned(Ini1)) then
Ini1 := TInifile.Create(IFILE);

try
Ini1.WriteString('xyz','a','TEST');
finally
Ini1.Free;
end;

end;

Comments

  1. Only managed types (like strings) are initialized, all other types including objects (in non-ARC) has unpredictable initial value.

    ReplyDelete
  2. the value of ini1 is undefined 

    procedure IniWrite(NilFirst: boolean);
    var
      Ini1: tinifile;
    begin
      Ini1 := TInifile.Create(IFILE);
      try
        Ini1.WriteString('xyz','a','TEST');
      finally
        Ini1.Free;
      end;
    end;

    ReplyDelete
  3. That is as designed and documented http://docwiki.embarcadero.com/RADStudio/Seattle/en/Variables

    "On the Wiin32 platform, the contents of a local variable are undefined until a value is assigned to them."

    ReplyDelete
  4. This question is not specific to TIniFile. Try and ask it again for, say, Integer.

    procedure foo;
    var
    i: Integer;
    begin
    // what is the value of i at this point?
    end;

    ReplyDelete
  5. http://docwiki.embarcadero.com/RADStudio/Seattle/en/Variables
    On the Wiin32 platform, the contents of a local variable are undefined until a value is assigned to them.

    ReplyDelete

Post a Comment