I would like to know WHY do we use “Finalization” if we want to destroy something when closing the application? doesn’t closing the application frees all objects directly without calling .Free?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No. Delphi class instances are not garbage collected and so they need to be manually destroyed.
However, if you are talking about an executable process, then it can be perfectly acceptable not to dispose of certain objects since the operating system will re-claim all resources owned by a process when that process terminates. So even though Delphi destructors don’t run, the OS tidies everything up when a process terminates. It is not possible for a process to leak any system resources once it has terminated.
Note that if the unit is included in a DLL or a package then failure to destroy all objects at finalization time will lead to memory leaks, if that DLL is repeatedly loaded and unloaded into a single process.
If you know that your code only ever runs in an executable, then feel at liberty not to
Freeobjects at finalization time. Be aware that if you are using a memory leak detection tool then doing so will result in your intentionally leaked object being treated as a memory leak. Deal with that by callingRegisterExpectedMemoryLeak.One final point to make is that an object’s destructor sometimes does more than free memory. Sometimes it can save values to a settings file, or the registry, for example. Naturally you would not want to omit running the destructor for such an object.