Initialization concepts lazarus / 31-LCL ---------------------------------------------------------------------- kupferstecher · 2018-03-26 11:11 Dear all, although I already use Lazarus for some years now, I have kind of a beginners question. I.e. how is the initialisation sequence for the user classes normally done? More in detail: Units often need some initialization work, like allocating memory (i.e. creating instances), initing variables, loading file data etc. Such initialization sequences are normally bundled within on class, e.g. in the Constructor(s) or a dedicated initialization procedure. Now there are several places from which the initialization procedures may be called. 1. The INITIALIZATION section of the unit. 2. The OnCreate event of the application form. 3. Between Application.CreateForm and Application.Run in the lpr-file In a larger application I currently use the 3. way. I have a unit uInit with procedure GlobalInit in which the initialization of each unit is called, so the lpr-file looks like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  Application.Title:='AProject';  RequireDerivedFormResource := True;  Application.Initialize;  Application.CreateForm(TMainForm, MainForm);  GlobalInit;  // <- Initialization  Application.Run;end. Why I don't use the INITIALIZATION sections of each unit? The problem is, that several units depend on each other, one unit requires an other unit to be initialized before, because the other unit is used by the one unit. And I don't know in which sequence the INITIALIZATION sections are called, probably not in the for me correct sequence. Why I don't use the form.OnCreate? I got problems initializing some controls when create of the main form is not finished. For some initialization works I even had to wait for the first application idle state otherwise some strange behaviour, like wrong dimensions, occured. But the third option I currently use also has its drawbacks: - Non intuitive (From where is the unit initialized?) - Easy to forget initialization of copied units (-> crash) - Deinitialisation also has to be done, but where? So my question is, how is it done normally? What is the intended way of program initializations? Thanks~ ---------------------------------------------------------------------- howardpc · 2018-03-26 11:31 If you have a main form and a secondary form, then the initialization sequence is as follows: MainForm initialization SecondaryForm initialization TMainForm.Create TSecondaryForm.Create GlobalInit TMainForm.Show So place your initializations appropriately. ---------------------------------------------------------------------- kupferstecher · 2018-03-26 11:50 Hello howardpc, I don't totally understand. Application.Initialize is calling the initialization sections of each unit, right? You agree to the way I'm doing it (GlobalInit)? The forms themselves is not the problem, I think. Normally everything can be covered in Form.OnCreate. But a lot of units need initialisations. These units then place their controls on the main form (created on runtime). Perhaps the problem also lies in the program structure. ---------------------------------------------------------------------- howardpc · 2018-03-26 12:27 No, Application.Initialize simply initializes the Application object (you can look at the sources to see what it does). The form(s) initialization code (and any non-form unit initialization code you insert) is called based I think on the order you cite the various units in the project's uses clause, but others may correct me about that. Anyway, unit initializations are not dependent on having an Application object. Otherwise, how could you have a console app with multiple units? ---------------------------------------------------------------------- kupferstecher · 2018-03-26 14:04 I see. And its understandable, INITIALIZATION is a freepascal feature and not from LCL. I checked the documentation: "The initialization block is used to initialize certain variables or execute code that is necessary for the correct functioning of the unit. The initialization parts of the units are executed in the order that the compiler loaded the units when compiling a program. They are executed before the first statement of the program is executed." https://freepascal.org/docs-html/ref/refse106.html So you were right about the execution order, as well. I think its not so helpful that the INITIALIZATION sections are called before the MainForm is initialized, as I build up the GUI on the (existing) Form and not the other way round. ---------------------------------------------------------------------- Leledumbo · 2018-03-26 17:57 Quote from: kupferstecher on March 26, 2018, 04:04:22 pmI think its not so helpful that the INITIALIZATION sections are called before the MainForm is initialized, as I build up the GUI on the (existing) Form and not the other way round. initialization section doesn't even know that forms ever existed. Library depends on language, not the other way around. If you need to build something (once) on top of a form that's been (being?) initialized, there's no better place than the OnCreate event. But it looks like you're trying to put all initialize-lookalike code there, which might not work depending on what you're initializing. So there's no generic solution, it has to be handled case by case (sometimes it's also a misuse, such as setting dimensions at OnCreate while having OnResize for the specific control which will override what's done at OnCreate). One thing to note, initialization order is predictable, so you can start thinking based on that fact. ---------------------------------------------------------------------- kupferstecher · 2018-03-26 18:54 Quote from: Leledumbo on March 26, 2018, 07:57:49 pminitialization section doesn't even know that forms ever existed. Library depends on language, not the other way around.Sure. Still it could be possible, that the MainForm is created first (e.g. in the first initialization section). I know, this isn't for discussion... QuoteBut it looks like you're trying to put all initialize-lookalike code there, which might not work depending on what you're initializing.Yes, I try now to move all initializations that aren't controls depending on the main for to the initialization sections to clean up a bit. The funny thing is, the initializations that work in the Initialization sections also work in the OnCreate. Quote So there's no generic solution, it has to be handled case by case (sometimes it's also a misuse, such as setting dimensions at OnCreate while having OnResize for the specific control which will override what's done at OnCreate).One thing I encountered is that the dimensions in the form's or the control's OnCreate are not defined, yet. When I try to create child controls that have anchoring and I init their dimensions, then it will get a mess, the anchoring will break. ---------------------------------------------------------------------- jamie · 2018-03-26 22:22 Its my understanding that all  UNITS listed in the USES clause in the MAIN unit get their INITIALIATION sections executed first, in the order in which I can't remember, I think the last one in the list gets called first, just like the scoping rules, I could be the other way around but I'll stick with that for now..    You can use use the BEGIN ... END at the end of the UNITS which is the original way of doing this, and it works like like using INITIAILATION execution, the only problem with this is, it does not supply a FINALIATION...  I believe the code is reverse order when the FINALIATION code blocks are executed at program end and that does not happen until your MAIN unit file hits the " END."   Anyways, the code you see in the MAIN unit does not start at the BEGIN until all the units have been initiated .  So care must be taken to not call any class code or any items sitting on the forms from those sections because they don't exist yet, you can however create some class code while in the INTIAL blocks of all units. ---------------------------------------------------------------------- taazz · 2018-03-27 13:06 Quote from: kupferstecher on March 26, 2018, 08:54:29 pmQuote from: Leledumbo on March 26, 2018, 07:57:49 pminitialization section doesn't even know that forms ever existed. Library depends on language, not the other way around.Sure. Still it could be possible, that the MainForm is created first (e.g. in the first initialization section). I know, this isn't for discussion... No its not. Do not create forms in the unit initialization section the errors you will get range from unable to load your application to random and extremely hard to truck. Forms depend on the Application object in the forms unit be initialized properly to function. Quote from: kupferstecher on March 26, 2018, 08:54:29 pmQuoteBut it looks like you're trying to put all initialize-lookalike code there, which might not work depending on what you're initializing.Yes, I try now to move all initializations that aren't controls depending on the main for to the initialization sections to clean up a bit. The funny thing is, the initializations that work in the Initialization sections also work in the OnCreate. all sorts of alarms going of in this statement, but not knowing the specifics you are referring to I'll not enumerate all the possible problems. In general the only initializations that are ok to do in the initialization section of the unit are unit (local or global) variables nothing else.