Suppose an application with some forms and only one data module is created at startup. In the DM1.OnCreate event, a TStringList is created that will be used at runtime. We know that when the application is terminated, all things will be destroyed, and the memory will be automatically freed. Releasing something may take some time, and therefore it is not always recommended to worry about a memory leak when shutting down. See for example this answer from Barry Kelly or this post from Raymond Chen.
Also, FastMM reports a memory leak if I do not add TStringList.Free to DM1.OnDestroy . This proves to be a problem when looking for any other memory leaks that I really have to worry about.
So, basically I ask if / why / when I should free instances of objects that will be freed by the application or OS (Windows in this particular case). Is there any other valid case that is not used when searching for memory leaks?
NOTE In this particular case, the data module is not created or recreated more than once. In addition, there will be no memory leak at all. Scrap source for data module:
unit UDM1; interface uses SysUtils, Classes, ...; type TDM1 = class(TDataModule) procedure DataModuleCreate(Sender: TObject); procedure DataModuleDestroy(Sender: TObject); procedure DoStuffWithStringList1(Sender: TObject); private internalStL: TStringList; end; var DM1: TDM1; implementation procedure TDMInterfacePAFECF.DataModuleCreate(Sender: TObject); begin internalStL := TStringList.Create(); end; procedure TDMInterfacePAFECF.DataModuleDestroy(Sender: TObject); begin internalStL.Free; //<-- IS THIS NECESSARY OR ADVISED? end; procedure DoStuffWithStringList(Sender: TObject); begin //Place some code using internalStL here... end;
memory-leaks free delphi fastmm
EMBarbosa
source share