[fpc-pascal]self

memsom at interalpha.co.uk memsom at interalpha.co.uk
Wed Oct 23 11:20:31 CEST 2002


Hello,

> type t = object
> procedure p(s:t); 
> end;
> procedure t.p(s:t); 
> begin
> self:=s;
> end;

Shouldn't 'procedure p(s:t);' actually have a pointer to 't' rather than a 
stack allocated 't'?

This is extremely bad coding imho. You are basically throwing away 'self' and 
creating a memory leak. It's a good thing (tm) if the compiler *is* telling you 
that it's illegal, even better if it is an error.

consider this:

 type 
   t = class  //altered from object so I don't have to remember the syntax
     procedure p(s:t);
   end;

   procedure t.p(s:t);
   begin
     self:=s;
   end;


   ...
   var
     a, b: T;
   begin
     a := T.create;
     b := T.Create;

     a.p(b);
     //Q: what does 'a' now point to? A: 'b'.
     //Q: where does the memory referenced by 'a' point to?
     //A: nothing. This causes a memory leak.
   end;
   ...

> It causes 'Illegal expression' where self is assigned.
> Does it mean that with the new version self is read only ?

Self should be read-only. Every instance has it's own 'self' and it makes no 
sense to alter what is points to.

What are you trying to achieve by re-assigning the value of 'self' anyway?


---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/






More information about the fpc-pascal mailing list