2

I created a class in delphi. It looks like this

    TGoJsNode = class(TPersistent)
      private
        _id: Integer;
        Key: Integer;
        Text: String;

        constructor Create;
    end;

I need to check when this class have been effectively created (Used the constructor). Example, when I do this:

var
  x: TGoJsNode;
begin
  if Assigned(x) then
     // Will return True
     showmessage('created');

  if x = nil then   
     // Will also return True
     showmessage('created');
end;

Both of these tests will result in true, when in fact i did not created x. In my opinion x should only result true in the assigned test when i create it, like so:

procedure TForm1.Button18Click(Sender: TObject);
var
  x: TGoJsNode;
begin
  x := TGoJsNode.Create;
end;

Another weird thing I observed is that the private numeric variables have random values and the string one is empty. All the classes are in the same Unit file. Any Ideas?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
CS Matheus
  • 49
  • 6

1 Answers1

4

Everything is behaving as designed and as expected. The problem is that your expectations are based on incorrect understanding.

Unmanaged local variables are uninitialized, and so their values are undefined until they have been explicitly initialized. So your variable x can have any value until you initialize it.

The Assigned() function tests whether or not a pointer variable is nil. Until you initialize the variable, the value returned by Assigned() is undefined. See this answer for a broader discussion on Assigned().

An exception to the rule that local variables are not initialized is managed types. They are always initialized. Strings are managed types, which explains why you see that they are always empty. Numeric types are not managed, and so will not be initialized automatically.

Other managed types include dynamic arrays, interfaces, variants, anonymous methods, and ARC classes on mobile platforms. Additionally, structured types that contain managed types, for instance records and arrays, themselves become managed.

Another nuance is that global variables and class members are default initialized. It is only local variables that may not be initialized.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I've lost count of the times that question of mine and answer of yours has been referenced. I even recall you mentioning it on your user profile. – Jerry Dodge Aug 06 '19 at 20:49
  • @RemyLebeau Thanks for the edit. I take very minor issue with the word random. Although it can seem random the value can perfectly well be deterministic and determined by the content of the stack left by earlier code. I prefer not to use the word random here because then the next question is, the value is the same every time I run the function, but you said it would be random..... – David Heffernan Aug 06 '19 at 21:02
  • And although strings are always valid, they are not necessarily empty, I found. – GolezTrol Aug 06 '19 at 21:47
  • 1
    @Golez They are always initialized to the empty string. If you find one that has a non empty value, something in your code assigned to that variable. – David Heffernan Aug 06 '19 at 21:53
  • Thank you @DavidHeffernan, the links helped me a lot – CS Matheus Aug 07 '19 at 11:20