Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>NO.</strong> You can try, you can hope but there is <strong><em>no guarantee</em></strong> in order of calling initialization and finalization. See <a href="http://qc.embarcadero.com/wc/qcmain.aspx?d=72245" rel="nofollow noreferrer">qc72245</a>, <a href="http://qc.embarcadero.com/wc/qcmain.aspx?d=56034" rel="nofollow noreferrer">qc56034</a> and <a href="http://www.google.com/search?q=unit+initialization+order+site%3Aqc.embarcadero.com" rel="nofollow noreferrer">many more</a>.</p> <p><strong>UPDATE:</strong></p> <ol> <li>finalization section is executed in reverse order than initialization. Your example is safe, you don't have dependency on calling initialization sections between units</li> <li>Delphi can mix calling units (point 1 is still valid, both initialization and finalization sections are swapped)</li> </ol> <p>Example:</p> <pre><code>unitA // no dependency on unitB var SomeController; initialization SomeController := TSomeController.Create; finalization SomeController.Free; unitB uses unitA; initialization SomeController.AddComponent(UnitBClass); finalization SomeController.RemoveComponent(UnitBClass); </code></pre> <p>Common (correct) order (99.99%) of calling:</p> <ol> <li>unitA.initialization</li> <li>unitB.initialization</li> <li>run...</li> <li>unitB.finalization</li> <li>unitA.finalization</li> </ol> <p>But <em>sometimes</em> can Delphi compile file wrong:</p> <ol> <li>unitB.initialization - AV here</li> <li>unitA.initialization</li> <li>run...</li> <li>unitA.finalization</li> <li>unitB.finalization - and here too</li> </ol> <p>Little offtopic story:</p> <p>We have quite big project, with Type1 in Unit1, Type2 = class(Type1) in Unit2. Files are ordered in project.dpr and after years and adding Unit200 (<em>no dependency with unit1/2</em>) Delphi starts compiling project with Unit2.Initialization before Unit1.Initialization. Only safe solution is calling your own Init functions from initialization section.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload