[fpc-pascal]Constructor failing...

paradice paradice at paradicesoftware.com
Wed Mar 19 06:17:12 CET 2003

Hi again -

another question; what is the best way to "fail" a constructor call? For
example, I have a constructor "timage.loadimage()", and I want it to return
"nil" if it encounters a problem (e.g. the file name doesn't exist). My
example program is this:

   mytestclass    =  class
      bobbob      :  longint;
      constructor    create;

constructor mytestclass.create;
   inherited create;
   free; // *** what do I put here?
   result := nil;

   bob   :  mytestclass;

   bob := nil;
   writeln('Memavail before: ',memavail);
   bob := mytestclass.create;
   writeln('Memavail after : ',memavail);
   writeln('Bob assigned? ',assigned(bob));

The problem is where the *** is marked; I don't know what to place there to
do everything properly. If I use "free" then there is no memory leak, but
"bob" is "assigned" (the result:=nil does nothing). I also tried "fail" (as
I remember seeing it in the docs somewhere but I couldn't find it again) -
if I do this bob returns "nil" as expected but there is a memory leak. Using
both of them together doesn't help me either. Is there a legal way to do

More information about the fpc-pascal mailing list