All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.delphi.rules.community-delphi.DestructorName.html Maven / Gradle / Ivy

There is a newer version: 1.12.1
Show newest version

Why is this an issue?

When objects are freed, the Destroy destructor method is called. Destroy must be an override (or virtual) method.

All classes inherit from TObject, which declares a virtual Destroy method, and should override this method to provide their own implementation.

Not naming the destructor Destroy will cause it not to be called when the object is freed. The same thing happens if Destroy is not an override, because the destructor is invoked polymorphically when the object is freed.

How to fix it

Ensure the destructor is named Destroy and marked with the override directive.

type
  TNode = class(TObject)
  private
    FChildNode: TNode;
  public
    destructor CustomDestroy;
  end;

destructor TNode.CustomDestroy;
begin
  FChildNode.Free;
end;
type
  TNode = class(TObject)
  private
    FChildNode: TNode;
  public
    destructor Destroy; override;
  end;

destructor TNode.Destroy;
begin
  FChildNode.Free;
  inherited;
end;

Note that in the "before" case above, FChildNode.Free would not call the destructor on TNode, causing a memory leak.

Resources





© 2015 - 2024 Weber Informatics LLC | Privacy Policy