Delphi - Maintaining Self References to an Object

随声附和 提交于 2019-12-24 16:53:27

问题


I've looked at many questions and resources which deal with the "Self" variable in an Object, but everyone says something different.

For example, in this question: Delphi Self-Pointer usage

the highest rated answer to the question appears to be wrong. Pointer(Self) does not point to the object which contains it, and cannot be used to pass references from inside the object.

I've tried doing this:

Type
  myobject = class
    PSelf: Pointer;
  End;

Var
  Obj: myobject;

Procedure RunProgram;
Begin
  Obj := myobject.create;
  Obj.PSelf := @Obj;

  //Run the rest of the program
  .
  .
  .

and for the most part, this has worked just fine.

My question is: is this a good coding practice? Can the "PSelf" variable be expected to point to the object for the duration of the program's execution?

I recently came across a bug where "PSelf" had stopped pointing to it's containing object, and I'm wondering if objects ever get shuffled around in the heap, or whether the memory had been corrupted.

Edit:

There is some instance in which using the "Self" variable didn't work for me, and now I cannot duplicate it. So this whole question is pointless, as is my technique of using a 'PSelf' variable. Sorry about that.

And as Ken pointed out, the link above has a correct answer :)


回答1:


I think you're misunderstanding what Self is, and how object references work in Delphi.

A variable containing an instance of a class is already a pointer to that object instance. The Delphi compiler just allows you to leave out the dereference operator (^) as a convenience.

var
  MyObject: TMyObject;  
begin
  MyObject := TMyObject.Create;  // MyObject is now a pointer to an instance of TMyObject
  ...

Delphi also allows the shorthand of not using the dereference operator when accessing members or properties of the object instance. Again, the following code is actually equivalent:

MyObj.SomeProperty := SomeValue;
MyObj^.SomeProperty := SomeValue;

From the Delphi documentation:

A variable of a class type is actually a pointer that references an object. Hence more than one variable can refer to the same object. Like other pointers, class-type variables can hold the value nil. But you don't have to explicitly dereference a class-type variable to access the object it points to. For example, SomeObject.Size := 100 assigns the value 100 to the Size property of the object referenced by SomeObject; you would not write this as SomeObject^.Size := 100.

Self is an automatically declared property that points to the current instance of the object. In other words, it's automatically available inside the code that implements that class to reference the current instance of the object. This allows you to have multiple instances of the same object:

type
  TMyObject=class(TObject)
  private
    FMyInteger: Integer;
    function GetMyInteger: Integer;
    procedure SetMyInteger(Value: Integer);
  published
    property MyInteger: Integer read GetMyInteger write SetMyInteger;
  end;

...
function TMyObject.GetMyInteger: Integer;
begin
  Result := Self.FMyInteger; 

  // Self is implied, so the above line can more simply be written as
  // Result := FMyInteger;
end;

procedure TMyObject.SetMyInteger(Value: Integer);
begin
  if (Value <> Self.FMyInteger) then  // Self is again implied here
    Self.FMyInteger := Value;
end;

var
 MyObjOne, MyObjTwo: TMyObject;
 i, j: Integer;
begin
  MyObjOne := TMyObject;
  // Here, the code inside TMyObject.SetInteger that
  // uses `Self` would refer to `MyObjOne`
  MyObjOne.MyInteger := 1; 

  MyObjTwo := TMyObject;
  // Here, the code in TMyObject.SetInteger would be
  // using the memory in `MyObjTwo` when using `Self`
  MyObjTwo.MyInteger := 2; 
end;        

Note that Self is only valid in the code that implements the class. It's available and valid in TMyObject.GetMyInteger and TMyObject.SetMyInteger above (the only implemented code in my example), and always refers to the current instance.

There's no need to keep track of the addresses of Self, as the variable referencing that object instance is Self inside methods of that object instance. It's only valid inside that instance of the object, and always refers to that object instance. So in your code example, PSelf is just wasted space - myobject already contains a pointer to itself, and that pointer is automatically available in methods of myobject:

type
  myobject = class;   // Now automatically contains a `Self` pointer
                      // available in methods of the class once an
                      // instance is created

var
  myobj: myobject;
begin
  myobj := myobject.Create;  // myobj.Self now is valid in methods of
                             // `myobject`



回答2:


Here's my resolution to the problem. I'm still wondering why the second example doesn't work.

This works (but is the wrong way to do it):

Type
  myobject1 = class(TObject)
    PSelf: Pointer;
    Number: Integer;
    Function GiveReference: Pointer;
  End;
  pmyobject1: ^myobject1;

  myobject2 = class(TObject)
    p: pmyobject1;
  End;

Var
  Obj1: myobject1;
  Obj2: myobject2;

Function  myobject1.GiveReference: Pointer;
Begin
  Result := PSelf;
End;

Procedure RunProgram;
Var
  i: Integer;
Begin
  Obj1 := myobject1.create;
  Obj1.PSelf := @Obj1;

  Obj2 := myobject2.create;
  Obj2.P := Obj.GiveReference;

  //to access 'number', this works
  i := Obj2.P^.Number;

  //Run the rest of the program
  .
  .
  .

This does not work, but in my mind is exactly the same. This is what caused me to distrust the 'self' variable (even though yes, I was making a pointer to a pointer).

Type
  myobject1 = class(TObject)
    Number: Integer;
    Function GiveReference: Pointer;
  End;
  pmyobject1: ^myobject1;

  myobject2 = class(TObject)
    p: pmyobject1;
  End;

Var
  Obj1: myobject1;
  Obj2: myobject2;

Function  myobject1.GiveReference: Pointer;
Begin
  Result := @Self;
End;

Procedure RunProgram;
Var
  i: Integer;
Begin
  Obj1 := myobject1.create;

  Obj2 := myobject2.create;
  Obj2.P := Obj.GiveReference;

  //This will fail, some of the time, but not all of the time.
  //The pointer was valid while 'GiveReference' was being called, but
  //afterwards, it is not valid.
  i := Obj2.P^.Number;

  //Run the rest of the program
  .
  .
  .

And finally, this is what I should have been doing all along:

Type
  myobject1 = class(TObject)
    Number: Integer;
    Function GiveReference: Pointer;
  End;

  myobject2 = class(TObject)
    p: myobject1;
  End;

Var
  Obj1: myobject1;
  Obj2: myobject2;

Function  myobject1.GiveReference: Pointer;
Begin
  Result := Self;
End;

Procedure RunProgram;
Var
  i: Integer;
Begin
  Obj1 := myobject1.create;

  Obj2 := myobject2.create;
  Obj2.P := Obj.GiveReference;

  //No problems with this, although I would generally check if P was assigned prior to
  //reading from it.
  i := Obj2.P.Number;

  //Run the rest of the program
  Obj1.Free;
  Obj2.P := nil;

I hadn't done this in the first place, because I was concerned that by not using a pointer, that I might actually be duplicating the entire object.



来源:https://stackoverflow.com/questions/14027244/delphi-maintaining-self-references-to-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!