I just found this in a TForm, that is used as a DataModule

I just found this in a TForm, that is used as a DataModule

procedure Tvars.FormDestroy(Sender: TObject);
begin
  IBDatabase1.destroy;
  IBTransaction1.destroy;
  IBQuery1.destroy;
  DataSource1.destroy;
  IBQuery2.destroy;
  DataSource2.destroy;
  IBQuery3.destroy;
  ...
end;

this goes on for about 50 of the Non-Visual components that are dropped on this form.

Why would someone do this?

Comments

  1. And why .destroy instead of .Free.

    ReplyDelete
  2. They could have done that with a for x:= to componentCount-1 and just destroy or free the IB components

    ReplyDelete
  3. Maybe in earlier version all these components was created at runtime.

    ReplyDelete
  4. My answer is:  because the developer doesn't understand Delphi's component model.  They don't get that those components will be freed naturally by the form (i.e., owner).  In fact, I'm surprised it doesn't cause an access violation.

    The only way this makes sense is if those components are manually created and not parented/owned by the form.

    ReplyDelete
  5. He's not using Nick's FreeAndNil from what I see. ;)

    ReplyDelete
  6. FYI: all components are defined in the DFM and the form is created normally in the dpr with Application.CreateForm and there is no access violation on application exit.

    I have a feeling that I'll get more of these kind of "anti-patterns" from this project. :-)

    ReplyDelete

Post a Comment