Does the Null Owner argument hold for dynamically created instances of the TComponent OK class?

I work with C ++ RAD Studio and Builder 6 quite often and often create forms dynamically or dynamically; create non-visual components when writing non-visual code. When developing forms, the property of the owner of the components that fell on this form is automatically set, so I never worried about it. However, when creating anything that comes from the TComponent dynamically, I always pass NULL as an Owner argument, since I always take responsibility for freeing the memory later.

The Borland / Embarcadero documentation does not actually cover what is required when things are dynamically created (or maybe I don't look in the right places) and only seems to ever cover scenarios based on development time.

I would like to know if dynamically created components need to be passed to the NULL owner, or if this could lead to internal problems that will come up later. The code compiles and works fine, but I am wondering if this is the cause or potentially causing problems behind the scenes.

+4
source share
1 answer

This, of course, is not the wrong thing.

Transferring the owner, as you have already hinted, eliminates the need to manage the lifetime of the object yourself. There are no hidden internal side effects that require transmission to the owner.

FWIW: Creating / using and destroying orphan components is not uncommon, we do it all the time.


Change cudo for Remy

While the components that come with your installation can be used without an owner (apart from some corner cases, such as TXMLDocument, which act differently with the appointed owner), there is always the possibility of a third party or home brew that relies on the designated owner.

+4
source

All Articles