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);
...
procedure FreeAndNil(var Obj);
var
Temp: TObject;
begin
Temp := TObject(Obj);
Pointer(Obj) := nil;
Temp.Free;
end;
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
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?
----------------
George Birbilis ([EMAIL PROTECTED])
Microsoft MVP J# for 2004-2006
Borland "Spirit of Delphi"
* QuickTime, QTVR, ActiveX, VCL, .NET
http://www.kagi.com/birbilis
* Robotics
http://www.mech.upatras.gr/~Robotics
http://www.mech.upatras.gr/~robgroup
> -----Original Message-----
> From: George Birbilis [mailto:[EMAIL PROTECTED]
> Sent: Monday, June 19, 2006 5:57 PM
> To: [email protected]
> Subject: RE: [lazarus] procedure TObject.Free
> Importance: Low
>
> Indeed, thanks for the sample. With Delphi7 you get same
> output as the output for FPC you got. Also at the modified
> version you suggested you get the error "Operator not
> applicable to operand type" (at the "self is TA"
> line) at compile time, so it's similar behaviour
>
> I wonder if I mixed it in my mind with Java. Now that I think
> of it there's a chance that in Java if it allows you to call
> a static method upon an instance of a class, the "this"
> pointer has a value, but if you call it upon the class itself
> the "this" pointer is null. I may be wrong with that too,
> will have to try it and see (if that's wrong too I don't
> remember how the original wrong info on class method
> behaviour in Delphi got in my mind - it keeps no backtracking
> info unfortunately ;-)
>
> greetings,
> George
>
> > This is not true for FPC (test it), and I can't imagine it would be
> > true for Delphi. Also see my other mail.
> >
> > No matter whether you call a class method on a class or on an
> > instance, 'self' will always be a class, not an instance!
> >
> > The following program
> >
> > {$MODE OBJFPC}
> >
> > type
> > TA = class
> > class procedure Fn;
> > end;
> >
> > class procedure TA.Fn;
> > begin
> > if self=TA then writeln('self=TA');
> > end;
> >
> > var
> > A:TA;
> >
> > begin
> >
> > TA.Fn;
> >
> > A:=TA.Create;
> > A.Fn;
> >
> > A.Destroy;
> >
> > end.
> >
> > outputs
> >
> > self=TA
> > self=TA
> >
> > Changing TA.Fn to
> >
> > class procedure TA.Fn;
> > begin
> > if self=TA then writeln('self=TA');
> > if self is TA then writeln('self is TA');
> > end;
> >
> > gives (correctly!) the compiler message
> >
> > classmethods.pas(11,13) Error: class type expected, but
> got "Class
> > Of TA"
> > classmethods.pas(11,13) Error: Incompatible type for arg
> no. 2: Got
> > "Class Of TA", expected "TObject"
> >
> > Regards,
> >
> > Bram
> >
> > _________________________________________________________________
> > To unsubscribe: mail [EMAIL PROTECTED] with
> > "unsubscribe" as the Subject
> > archives at http://www.lazarus.freepascal.org/mailarchives
> >
>
>
>
> _____
>
> avast! Antivirus <http://www.avast.com> : Outbound message clean.
>
>
> Virus Database (VPS): 0625-1, 19/06/2006 Tested on: 19/6/2006
> 5:57:18 ??
> avast! - copyright (c) 1988-2006 ALWIL Software.
>
>
>
> _________________________________________________________________
> To unsubscribe: mail [EMAIL PROTECTED] with
> "unsubscribe" as the Subject
> archives at http://www.lazarus.freepascal.org/mailarchives
>
_____
avast! Antivirus <http://www.avast.com> : Outbound message clean.
Virus Database (VPS): 0625-4, 21/06/2006
Tested on: 21/6/2006 2:46:22 ??
avast! - copyright (c) 1988-2006 ALWIL Software.
_________________________________________________________________
To unsubscribe: mail [EMAIL PROTECTED] with
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives