Al Boldi wrote:
Bram Kuijvenhoven wrote:
  function TAncestor.ToStringSafe:string;
  begin
    if self=nil then Exit('<nil>');
    Result:=ToString; // now it's safe to call the virtual method
  end;

Are you sure it's really safe to call?

I think so: function ToStringSafe:string; is not virtual. You can check it 
yourself by running the code.

BTW, is there a way to make Assigned(obj) really check for a valid pointer?

Not really. - When destroying an object, the pointer is invalid, but still points to a memory area with about the same data as before; how would you see the difference
- You could try and find a way to ask the memory manager whether there is a 
valid block at pointer^
- You could register each object in the constructor and unregister it in the 
destructor and then look up 'pointer' in the register (e.g. a hash table or avl 
tree) each time, but that would
 a) be inefficient, and
 b) come down in fact to an inefficient partial implementation of a garbage 
collector mechanism

Instead, you should just make sure the method is always called on a valid 
object pointer be the design of you software. If you can't do that, don't use 
Pascal or C.

Note that a try .. catch .. end block can be of help. When executing a piece of 
code that is not critical (like a GUI callback) you can use it to catch errors 
(and handle them!), such that your application does not simply terminate on 
such errors. But don't use empty except on ... handlers! That is really bad 
design.

Bram

_________________________________________________________________
    To unsubscribe: mail [EMAIL PROTECTED] with
               "unsubscribe" as the Subject
  archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to