Forum > LCL

Creating "TEdit" component dynamically

<< < (3/5) > >>

pascal111:
It seems Lazarus freePascal is strong language in event driven programming, it cares in details, small things of OOP and Visual Programming and different techniques.

y.ivanov:

--- Quote from: pascal111 on May 05, 2021, 02:59:06 pm ---I see now. I think the different between the two concepts can be used in advanced uses of components.

--- End quote ---

It is quite simple actually, you may think of objects as of instances of a certain class, and for that class as of the only instance of some hypothetical meta-class.
Therefore, all that is valid for the object can be applied to the class - the class also have its methods and data fields (static ones).
So, the constructors are actually methods of the class - they construct an instance of the object and return them back as a reference:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  e1 := TEdit.Create(Self);  Means "call the method Create on (sole) instance of Meta-TEdit named TEdit and assign the returned reference to the e1".  As lucamar said, the:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  e1.Create(Self);  means "call the method Create on instance of TEdit named e1", but e1 doesn't exist yet and also does not have such a method.

It's simple.

pascal111:
It has logical sense @y.ivanov. I understand.

Remy Lebeau:

--- Quote from: y.ivanov on May 05, 2021, 02:19:50 pm ---One more thing, since you gave an Owner to the TEdit by:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---e1:=tedit.Create(self);
You shall not free it at FormDestroy(). It will be freed by the Owner at the right time.

--- End quote ---

While it is true that an Owner will free the component automatically, it should be noted that it is perfectly safe to manually Free() a component that has an Owner assigned.  The component will simply remove itself from the Owner's internal list so that the Owner will not try to free the component again when the Owner is freed.

y.ivanov:

--- Quote from: Remy Lebeau on May 06, 2021, 12:30:13 am ---*snip*
While it is true that an Owner will free the component automatically, it should be noted that it is perfectly safe to manually Free() a component that has an Owner assigned.  The component will simply remove itself from the Owner's internal list so that the Owner will not try to free the component again when the Owner is freed.

--- End quote ---

Formally true. My wording must be read "You may not free it" instead of "You shall not free it" then.

On the other hand, giving a non-nil owner to a component on its constructor means that we're transferring the life-cycle management to that owner. If we intend to manage the component life-cycle by ourselves, it is much better to create it without owner. Mixing the two approaches will result in UAF sooner or later.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version