Why is the default object not equal to zero?

In Delphi, the documented behavior for variables descending from TObject is the default value of nil. However, I came across a situation where this is not the case.

Running the following code sample through the IDE (F9) gives mixed results

var
  objTemp : TMemDataSet;
begin
  if (objTemp = nil) then
     ShowMessage('Nil');
end;
  • 32 bit / debug mode, not default - nil
  • 32 bit / release mode, not default - nil
  • 64 bit / debug mode defaults to zero
  • 64 bit / release mode, not default - noil

My understanding is a value that should always be zero by default.

We also tested this under XE2 and XE5 with the same results.

Is this the expected behavior in Delphi?

+4
source share
2 answers

. ( IOW, non-reference-counted) . , .

XE5 (. " " - Wiin32, ):

, 0. () 0. Wiin32 undefined, .

Delphi 2007, ; " VBScript" " [OpenGL]".

, Win64, , - . . , , , . ; ,

var
  MyObj: TSomething;

, , :

MyObj := TSomething.Create;   // Created yourself
MyObj := GetSomething();      // Function result
MyObj := Self.SomethingCollection[Self.SomethingCount - 1]; // Local ref

, var, var :

if SomethingIGot = nil then
  raise Exception.Create('Received a nil parameter');
MyObj := SomethingIGot;

// or

MyObj := SomethingIGot;
if not Assigned(MyObj) then
  raise Exception.Create('MyObj was assigned a nil value');
+9

, , ...

Delphi , TObject, nil.

- : (.. TObject) 0 nil.
, , , .

, , Delphi 2.

:


() .
.
nil ( 0) - , - .

procedure Test;
var
  MyObject: TMyObject;
begin   
  MyObject:= TMyObject.Create;  
  .....

, , - -.
, , .

, .
, , , .


, .
Delphi . , TObject.Create - 0 ( nil).
, , Create ( TObject).

, ; .

.
-.
.
.

+1

Source: https://habr.com/ru/post/1536835/


All Articles