5

I work with C++ RAD Studio and Builder 6 quite a bit and often create forms dynamically or create non-visual components dynamically when writing non-visual code. When designing forms, the Owner property of components dropped onto that form is automatically set so I have never worried about it. However, when creating anything derived from TComponent dynamically I always pass NULL as the Owner argument as I am always taking responsibilty for freeing the memory later.

Borland/Embarcadero documentation doesn't really cover what is required in the case of dynamically creating things (or maybe I have not been looking in the right places) and only seems to ever cover design time form based scenarios.

I would like to know if passing a NULL owner for dynamically created components is the right thing to do, or if it can lead to internal problems that will manifest themselves later. Code compiles and works ok but I wonder if it is causes or potentially causes any behind the scenes problems.

Johan
  • 74,508
  • 24
  • 191
  • 319
mathematician1975
  • 21,161
  • 6
  • 59
  • 101

1 Answers1

5

It's certainly not a wrong thing to do.

Passing an owner, as you already hinted to, alleviates you from having to manage the lifetime of the object yourself. There are no hidden internal side-effects that require you to pass an owner.

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


Edit cudo's to Remy

While the components that ship with your installation are fine to use without an owner (beside some corner cases like TXMLDocument that act differently with an owner assigned) , there's always the possibility of a third party or home brew component that rely on the owner being assigned.

Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
  • Thanks for your answer. Yes I do it all the time too, but due to some recent issues that I simply can't pin down, I just wanted to make sure that there was nothing potentially problematic about doing it. The documentation on dynamic creation of objects/components is pretty sparse in RAD studio – mathematician1975 Apr 12 '13 at 10:02
  • @mathematician1975 - Not afaik but do you perhaps use third party or home brew components that rely on the owner being assigned? – Lieven Keersmaekers Apr 12 '13 at 10:38
  • 1
    There are indeed some 3rd party components that incorrectly use the Owner, but in general, 99.9999% of the time, it is perfectly fine to use a NULL Owner. – Remy Lebeau Apr 12 '13 at 22:02
  • Also some corner cases, like `TXMLDocument`, that act differently depending on whether an Owner is specified or not. – Remy Lebeau Apr 12 '13 at 22:07
  • @RemyLebeau what version of C++B did `TXMLDocument` first appear in? – M.M May 16 '14 at 04:08
  • @MattMcNabb: `TXMLDocument` was introduced in Delphi/C++Builder 6 Enterprise edition, with a [separate download](http://cc.embarcadero.com/Item.aspx?id=18938) to install it in the Professional edition. – Remy Lebeau May 16 '14 at 14:47
  • @RemyLebeau thanks .. could have saved myself some time instead of building Expat – M.M May 17 '14 at 04:13