Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does every Class we create in Delphi need to have destructor?

When I create my custom Class in Delphi Application I use standard procedure:

TCustomClass = Class
 private
  var1,var2 : integer/string/Real/Boolean...
  procedure P1...
  function F1...
 public
  constructor Create;
end;

...

CustomClass := TCustomClass.create;

I want to know do I always have to also create Destructor procedure or are resources automatically free when application closes?

I always use Application as owner, rarely Self or Nil and I don't remember I saw anyone declaring Destructor on classes I saw on Internet, except for one when someone used pointers.

Is the logic behind destructor same in Delphi as in C++ as described in this question:

Should every class have a virtual destructor?

Thanks in advance.

EDIT1: As Free Consulting mentioned I forgot to say that one of the variables might be TBitmap type

like image 865
Tomislav Horvat Avatar asked Oct 18 '22 08:10

Tomislav Horvat


1 Answers

It only needs to have a destructor if you need to clean up something, like allocated memory. For example, if you have used TClassname.Create(...) in the constructor, you need to free in the destructor method.

Of course there can be many different reasons to need a destructor (all the way up to informing the user that his data is about to get wiped), but this is the most common one.

like image 80
Jur Avatar answered Oct 21 '22 06:10

Jur