Delphi FreeAndNil: Looking for an alternate implementation

偶尔善良 提交于 2020-12-05 12:24:53

问题


NOTE: Bear with me, I feel a little "flame grilled" due to some discussions over here and here and some issues I reported here and here.

Some background

Ye olde (pre 10.4) FreeAndNil looked like this:

FreeAndNil(var SomeObject)

The new and fresh FreeAndNil looks like this:

FreeAndNil(const [ref] SomeObject: TObject);

IMO both have their downsides:

  • The old one doesn't do any type checking, so calling FreeAndNil on pointers, records and interfaces compiles just fine, but produces interesting but usually unwanted effects during runtime. (Goes completely berserk or if you are lucky it halts with EAccessViolation, EInvalidOperation etc.)
  • The new one accepts a const parameter, and therefore any object. But then the provided object pointer is actually changed using some hacky-wacky code.
  • You can now call the new FreeAndNil like this: FreeAndNil(TObject.Create) and it will compile and even run just fine. I liked the old FreeAndNil that warned me when I went wrong and provided e.g. a property instead of a field. Unsure what happens if you provide a object type property to this FreeAndNil implementation. Didn't try.

If we would change the signature into FreeAndNil(var SomeObject:TObject) then it will not allow us to pass any other variable type then exactly the TObject type. Which also makes sense, as if it weren't FreeAndNil, one could easily change a variable provided as type TComponent in the routine change the var variable into an object of a completely different type, e.g. TCollection. Of course FreeAndNil will do no such thing, as it always changes the var parameter to nil.

So this makes FreeAndNil a special case. Maybe even special enough to convince delphi to add a compiler magic FreeAndNil implementation? Votes anyone?

Potential work-around

I came up with the code below as an alternative (here as a helper method, but could as well be part of TObject implementation) which kind-a combines both worlds. The Assert will help finding invalid calls during runtime.

procedure TSGObjectHelper.FreeAndNilObj(var aObject);
begin
  if Assigned(self) then
  begin
    Assert(TObject(aObject)=self,ClassName+'.FreeAndNil Wrong parameter provided!');
    pointer(aObject):=nil;
    Destroy;
  end;
end;

Usage would be something like this:

var MyObj:=TSOmeObject.Create;
...
MyObj.FreeAndNilObj(MyObj);

I have actually tested this routine, and it even is slightly faster than the 10.4 FreeAndNil implementation. I guess because I do the assignment check first and call Destroy directly. What I do not like so much is that:

  • the type checking takes place during runtime, and then only if Assertions are ON.
  • it feels like having to pass the same variable twice. Which isn't necessarily true/required. It has to be the same object, and the parameter has to be a variable.

Another investigation

But wouldn't it be great if one could call without the parameter

var MyObj:=TSomeObject.Create;
...
MyObj.FreeAndNil;

So I messed around with the self pointer and managed to set it to nil using the same Hacky-Wacky code that 10.4 utilizes in their FreeAndNil. Well... that worked inside the method, self pointed to nil. But after calling FreeAndNil like this, the MyObj variable wasn't nil, but a stale pointer. (This was what I expected.) Moreover, MyObj could be a property or (the result of) a routine, constructor etc.

so nope over here as well...

And finally the question:

Can you think of a cleaner/better solution or trick that would:

  • FreeAndNil(var aObject:TObject) with not-so-strict type checking compile time (maybe a Compiler directive?) so it allows compiling and calling for variables of any object type.
  • Complains compile time when something is passed that is not a variable/field of some object type
  • Help describing what is the best solution/requirement in RSP-29716

回答1:


The only proper solution to FreeAndNil that is both type safe and does not allow freeing function results and properties would be generic var parameter:

 procedure FreeAndNil<T: class>(var Obj: T); inline;

But, currently Delphi compiler does not allow generics on standalone procedures and functions https://quality.embarcadero.com/browse/RSP-13724

Still, that does not mean you cannot have generic FreeAndNil implementation, only that it will be a bit more verbose than necessary.

type
  TObj = class
  public
    class procedure FreeAndNil<T: class>(var Obj: T); static; inline;
  end;

class procedure TObj.FreeAndNil<T>(var Obj: T);
var
  Temp: TObject;
begin
  Temp := Obj;
  Obj := nil;
  Temp.Free;
end;

Type inference introduced in Rio will allow you to call it without specifying generic signature:

TObj.FreeAndNil(Obj);

Calling (and using) generic FreeAndNil in older Delphi versions is also possible but even more verbose

TObj.FreeAndNil<TFoo>(Obj);



回答2:


Because we cannot create a global procedure FreeAndNil<T:class>(var aObject:T) I would suggest the code below as a method to the TObject class. (rtl change to be made by embarcadero, but does not need a compiler change)

class procedure TObject.InternalFreeAndNil(var Object:TObject); static; // strict private class method
begin
  if Assigned(Object) then
  begin
    var tmp:=Object;
    Object:=nil;
    tmp.Destroy;
  end;
end;


class procedure TObject.FreeAndNil<T:class>(var Object:TObject); inline; // public generic class method
begin
  InternalFreeAndNil(TObject(Object));
end;

and to have the current (10.4 and earlier) FreeAndNil removed from the sysutils unit to avoid ambiguity.

When the new generic FreeAndNil method is called from within any other method, one can simply call:

FreeAndNil(SomeObjectVariable)

and 10.3+ type inference avoids having to write:

FreeAndNil<TMyClassSpec>(SomeObjectVariable)

which is nice because most of your code will compile nicely without a change.

In some other spots, eg global routines and initialization / finalization sections one would have to call:

TObject.FreeAndNil(SomeObjectVariable)

Which to me would be acceptable, and a lot better than the current and historical half-way solutions with a FreeAndNil(const [ref] aObject:TObject) or an untyped FreeAndNil(var aObject)

And since the routine is so utterly simple and performance appears to be an issue, one could argue to have an assembler implementation for it. Though I am not sure if this is allowed/possible for generic, (and preferably inline) methods.

FTM: One could also just keep FreeAndNil(var aObject:TObject) and tell people to do a typecast like below, which also avoids the compiler complaining about the var type. But in this case, probably a lot of source code has to be adjusted. On the other hand it saves on code bloat, still avoids Invalid use of function results, properties or invalid types like records and pointers as parameter to FreeAndNil, and is utterly simple to change/implement.

...
var Obj:=TSomeObject.Create;
try
   DoSOmethingUseFulWithObj(Obj);
finally
  FreeAndNil(TObject(Obj)); // typecast avoids compiler complaining. Compiler wont allow invalid typecasts
end;
...


来源:https://stackoverflow.com/questions/62479148/delphi-freeandnil-looking-for-an-alternate-implementation

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