Is there another situation besides the search for memory leaks when I have to free all objects when the application is destroyed?

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; 
+8
memory-leaks free delphi fastmm
source share
5 answers

For the same reason, I strongly advocate (underestimation) that I did not leave any hints or warnings for the compiler in the project, I am clean after myself and DO NOT LEAVE MEMORY! EVER!

Now this does not necessarily mean that you need to free everything in the Destructor of your DataModule, if you have a strong argument not to do it, but in this case you need to register a memory leak so that it is not reported. (And add a very noticeable comment to justify and explain why)

But keep in mind the fact that you can leave this project in a year, someone else supports it and has a new business requirement for creating several DataModules ... Most likely, if they don’t know the inside of your code well enough, they will trust your code to be clean, and problems are likely to follow.

Therefore, I would strongly advise you not to release, if only in a very special and expected and documented case ...

PS: I saw it, and I often had to miss memory, downloading it all over the place, and I even did some CodeRage sessions to deal with memory leaks ...

Updayte: Here is the link to download this CodeRage session ...

+7
source share

My answer can be considered philosophical, but the main reason is that any action (or lack thereof) has consequences. I thought about your example, and possibly other examples, and I see one good reason for freeing the object. Every time I think that I can ignore a release object, the likelihood that it will not do this in another, perhaps more serious situation. Another example is the habit of doing “try to finish it completely” wherever something stands out or is freed. I do not care about exemption in the event of an exception, but this habit helps me avoid leakage.

+7
source share

Use RegisterExpectedMemoryLeak for intentional memory leaks. There are several overloaded versions in a routine that can be associated with an object class:

 begin RegisterExpectedMemoryLeak(TStringList); FStringList := TStringList.Create; ... 

or better to register the pointer itself:

 begin FStringList := TStringList.Create; RegisterExpectedMemoryLeak(FStringList); ... 

In this case, unexpected memory leaks will be displayed normally and cannot be confused with this particular string list.

+5
source share

Let me answer by asking you a question.

Can you say for sure that the life cycle of the data module will always be tied to the lifetime of the application, or that you should never create additional instances from this?

If you answer yes, feel free to ignore standard memory management methods.

If you answer no, you must make sure that the object is cleared after itself.

+4
source share

Things are done when you free an object, perhaps more than just freeing up memory.

A database object comes to mind that executes transactions and completes all started transactions in ondestroy.

If you do not call for free, ondestroy will not start, and you can end up with locked tables.

+2
source share

All Articles