Question Lifetime Of LayoutEngine Instance

NotchersMine

New member
Joined
Feb 23, 2017
Messages
2
Programming Experience
10+
Quick question (I hope):
Does a control's LayoutEngine component get re-instantiated on every call to Layout(), or does it remain throughout the lifetime of the control?

I am creating a custom LayoutEngine to be used for various controls. It has a number of fields that get assigned values on each Layout() call. I had created an internal helper class to store and set these values (this class was created for reasons that no longer apply), but I was thinking that if the LayoutEngine instance does not get instantiated/destroyed on each reference, I might keep on using the helper class and nullify it at the end of each layout to send it to the GC...

It's not that I have memory issues or anything, it's just that I suspect that Layout() is called for any container only a handful of times in the overall scheme of things, so why not keep it clean? Or is this point moot - should I even care?

Thanks in advance for any clarity you can provide on this!
 
Not sure if the question is correct, I think you mean LayoutEngine property. Layout method is an instance method of the LayoutEngine class, it can not create the instance it belongs to itself.

Control class has an overridable readonly property LayoutEngine and a Layout event with a protected overridable OnLayout method, this method by default just gets the object from LayoutEngine property of current instance and calls its Layout method, plus raises the Layout event.

In both the default Control implementation and most LayoutEngine property overrides I have seen a shared instance of the custom LayoutEngine class is returned, exposed from that class. One exception is the ToolStrip control where a private field is referenced, that is set either in its instance constructor or conditionally in LayoutStyle property (a shared instance or a new instance is used). Such custom controls often also override OnLayout method, where they do minor layout and call base OnLayout.
 
Thank you for taking the time to respond. What I meant by the question is: does the LayoutEngine property get called each time the Layout method is called, or is the property called once to get and store the instance and then use that instance for all Layout calls? However, in retrospect it was an irrelevant question because (as you indicated) the property normally returns a shared instance, which would remain for the life of all related controls.

The original reason that I created the helper class was because I call various methods from the Layout override, and rather than pass multiple references/values on each call I thought it would be better to pass a single reference to the helper class instance, which contains all the pertinent values (and some helper methods as well). The reason I said that the "class was created for reasons that no longer apply" was that I can integrate the values into the engine definition itself; but with a shared engine I need to isolate the instance data anyway, so the helper class makes sense.

Given all of that, I guess that my question should have been: would it make sense to nullify the helper class (and therefore the storage) at the end of my Layout method override? Or would the overhead of re-instantiating the helper class make it pointless?

I guess that I have gone off on a trivial tangent, and for that I apologize. But I thank you once again for your help!
 
Without having read your posts in detail, I think that you should be aware that it is possible to get access to the source code of the .NET Framework and even step through it while debugging. I suspect that could actually answer your own question by looking through the Framework source for yourself.
 
When you write a custom LayoutEngine you attach it to a control by overriding LayoutEngine property, this is where the instance is returned from. OnLayout gets LayoutEngine property value and calls Layout method on that, it does not intervene with the property, which always has the say on the object. Is common for the property to return a shared instance as I've seen in .Net framework classes, but if each control instance needs its own cached LayoutEngine data I guess it would make sense for the control to use its own LayoutEngine instance (as seen in some cases in ToolStrip control).
 
Back
Top