[fpc-pascal]Polymorphism of class field

Michael Van Canneyt michael.vancanneyt at wisa.be
Thu Aug 30 11:52:48 CEST 2001

> Before anyone jumps on me, this is tested under FPC 1.02 *and* FPC 1.04,
> compiled with '-S2'. The destructor never gets called if you call 'free'. It
> would be *really* nice if you could write this into the docs for 1.2, I expect
> it's already there, but it's obviously not evident coz people keep making this
> mistake. If you're going to allow people to use Delphi stle classes, they
> should always inherit from TObject (even if the user doesn't specify this) and
> therefore the destructor has to be called Destroy.

This is implicit: _ALL_ classes descend from TObject (it states so in the docs, I think)
So the 'Destroy' destructor must always be used.

But I will explicitly state this in the docs.

> Breaking this is really bad, unless calling 'done' directly does
> call 'destroy'. I note however that if I create a class with 2 destructors,
> firstly FPC1.04 complains that I do have 2, and secondly only 'done' gets
> called if I call 'done' and only 'destroy' gets called if I call 'free'.

This is correct behaviour, But maybe not the expected behaviour.

> Sorry to moan again, but this is something that will cause many people problems
> if it's not addressed. The simplest, cheapest and less painfull would be
> to 'warn' people if they are using 'class' style constructs and
> their 'destructor' is not called 'destroy'.

You *can* use a destructor that is not called destroy, as long as it calls
the parent destructor 'Destroy':

Destructor TMyObject.DoMyDestroy;


But it means that you must explicitly call the DoMyDestroy destructor.

When using 'Free' you must know what 'Free' does: It checks for Nil, and then
calls destroy -> ergo, if you don't call your destructor 'Destroy'
(thus overriding TObject's implementation), using Free will not call your destructor.

But the point is taken, and I will add a remark similar to the above, to the docs.


More information about the fpc-pascal mailing list