PDA

View Full Version : does delphi require memory freeing?



Paulius
26-11-2002, 08:22 PM
As far as i heard you don't have to worry about freeing memory in Delphi, but i was wondering at what moment is the memory freed. Lets say i create a couple of forms when the program ir running and i close one, is the memory used by the closed form freed when it is closed or when the whole application closes?

Michalson
26-11-2002, 08:45 PM
No, memory is *not* automatically freed in Delphi (no garbage collection). What you are experiencing works only with VCL components/objects. Every VCL component derives from the TComponent class. The TComponent constructor takes another object as its "owner", to which it registers itself (you can see which objects are "owned" by a particular class by reading the ComponentCount and Components property). Whenever a VCL object is freed it automatically frees all the objects that it owns, and informs its owner that it has been freed. In this way you can create a form object with no owner, then create a bunch of components (buttons, labels, etc) with the form as their owner. When the form is freed it automatically frees all the components attached to it (though you shouldn't confuse Owner with Parent).

Michalson
26-11-2002, 08:47 PM
And to answer your question better: When forms are created by Delphi they are given the TApplication object as their owner. This means that they will be freed either when you manually free them or when the Application.Free method is called (which happens when the program shutsdown).

Paulius
27-11-2002, 05:42 AM
Thanks

TheLion
27-11-2002, 06:59 AM
So if I understand you correctly, when I create a component without specifying an owner (nil) then the object won't be freed from the memory if the application is closed?

Useless Hacker
27-11-2002, 11:03 AM
So if I understand you correctly, when I create a component without specifying an owner (nil) then the object won't be freed from the memory if the application is closed?
Yup.

TheLion
27-11-2002, 11:22 AM
:shock:

That's nice to know... :)

Thanks for the info!