On 6/21/06, George Birbilis <[EMAIL PROTECTED]> wrote:
With all this discussion on whether FreeAndNil is class method or not, I
forgot to ask about my original comment on why FreeAndNil doesn't accept a
Tobject instead of an untyped var parameter.
From Delphi7's SysUtils.pas:
{ FreeAndNil frees the given TObject instance and sets the variable
reference
to nil. Be careful to only pass TObjects to this routine. }
procedure FreeAndNil(var Obj);
...
Couldn't they have used a "var Obj:TObject" instead? Would there be a
problem with passing "nil" objects to that proc then? I wonder why
No, because the compiler requires an the 'var' parameter is of exact
same type, it can't even be a descendant :-(
As it is now it does very bad crash if you pass by accident say an interface
reference to it instead of an object reference (can easily do this mistake
if one reengineers their code to use interfaces - they may forget to remove
the FreeAndNil stuff, thus getting bad runtime crashes instead of a compiler
message that a TObject descendent is needed as param to FreeAndNil)
Is FreePascal using "TObject" there or a "var Obj" param too?
I've just checked (version 2.0.2) and it indeed is implemented like Delphi :-(
But as a matter of fact it could have been implemented the right way,
because FPC does the sane thing and allow descendants as 'var'
parameters.
Yerder yet there's a 'FreeThenNil' function in LCLProc which calls
obj.Free before setting the pointer to null :-/
Cheers,
Flávio
_________________________________________________________________
To unsubscribe: mail [EMAIL PROTECTED] with
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives