Destructor without redefinition directive

What is wrong with this code:

type TobjAvisos = class public constructor Create; destructor Free; end; implementation constructor TobjAvisos.Create; Begin inherited Create; end; destructor TobjAvisos.Free; begin inherited Destroy; end; 

It compiles without warning, but FixInsight returns a warning: "W522 Destructor without redefinition directive"

+5
source share
1 answer

The problem is that you have to override the Destroy virtual destructor. This virtual destructor is what is called by the non-virtual Free method.

Be that as it may, the only way to destroy your class is to call the destructor directly. But it is expected that Delphi classes will be destroyed using the Free method.

Your class should look like this:

 type TobjAvisos = class public constructor Create; destructor Destroy; override; end; 

Overriding the Destroy virtual destructor is the only way to make your class valid using the Free method.

Now there are two main reasons for using Destroy virtual destructor and Free support:

  • Free can be safely invoked on the nil object link. The destructor cannot. This is important for the mechanism of constructing an object for handling exceptions.
  • Free support allows you to safely destroy an object, even if the runtime type of the object is more inferred than the compile time type of the object reference.

Some useful reading on related topics can be found here: Why shouldn't I use "if Assigned ()" before using or releasing things?

+11
source

All Articles