You can simply make use of Delphi's ownership mechanism. You can pass in an owner (of type TComponent
) in the constructor and then your data set will be destroyed whenever the owner is destroyed. In your case, just pass the form as the owner.
function TDM.CreateWorkstationsList(Owner: TComponent): TADODataSet;
begin
DS := TADODataSet.Create (Owner);
...
end;
DataSource.DataSet := DM.CreateWorkstationsList (Self);
You could also destroy the data set manually, for example in the form's OnDestroy
event.