[fpc-pascal] If an Assert fails in constructor, then destructor called (?)

Michael Van Canneyt michael at freepascal.org
Wed Jul 1 13:15:29 CEST 2009



On Wed, 1 Jul 2009, Tom Verhoeff wrote:

> On Tue, Jun 30, 2009 at 07:58:47PM +0100, Inoussa OUEDRAOGO wrote:
>> 2009/6/29 Tom Verhoeff <T.Verhoeff at tue.nl>:
>>> While tracing a nasty bug (?), I discovered the hard way that when
>>> an Assert is done in a constructor, and it fails, then the destructor
>>> (Destroy) is automatically called.
>>
>> Indeed, when an exception is raised in the constructor, be it an
>> "assert" exception or not, the destructor is called to allow the
>> developer to clean up the "in-construction" instance's members he has
>> already initialized.
>
> A colleague of mine pointed out that the Delphi 7 documentation for
> TObject.Create actually says so (see final Note):
>
>  Constructs an object and initializes its data before the object is first
>  used.
>
>  Delphi syntax:
>
>  constructor Create;
>
>  Description
>
>  Create constructs an object. The purpose, size, and behavior of objects
>  differ greatly. The Create constructor defined by TObject allocates memory
>  but does not initialize data.
>
>  Descendant objects usually define a constructor that creates the
>  particular kind of object and initializes its data.
>
>  Note: If an exception escapes from a constructor, the object's
>  destructor is called to clean up the failed instance.
>
> Zoiets zou ook in de TObject.Create (RTL) doc. van FreePascal passen
> (lees: thuishoren).

For non-dutch speakers: It says that this kind of information should
be included in the FPC documentation. I will do so. But in my opinion,
it does not belong in the unit reference documentation, but in the language
documentation.

Michael.



More information about the fpc-pascal mailing list