<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2016-04-12 17:15 GMT+02:00 Sven Barth <span dir="ltr"><<a href="mailto:pascaldragon@googlemail.com" target="_blank">pascaldragon@googlemail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span><p>> Maybe "run" for my idea is a bad word. We don't need to run any section before initialization section from system. What we need is the table/list with "fixed" variables which needs to be initialized in system.pp initialization (and finalized in finalization). I think that is doable.<br></p></span><p>It doesn't matter. You need to run the initializer operators and these might rely on functionality that is not initialized yet. So the safest approach would definitely be to init these after all initialization sections and to document this and the reasons behind it. This also alleviates the need to use attributes for this (or a keyword or whatever).</p></blockquote><div>Running the initializer operators after initialization is IMO bad idea... Very "pascal incompatible" and confusing even for me ;). I see many problems for that approach...<br></div></div><div class="gmail_extra"><br></div>I think I have other solution (that also can help to solve old problems). What is needed is new section like "managementinitialization" and "managementfinalization" (to complement "initialization"/"finalization" with a little different logic). managementinitialization should contains only module related stuff (memory allocation etc.) mostly used/required by Initialize management operators without dependencies nor complex logic. How it supposed to work? Below is attached modified example presented by <span style="color:rgb(80,0,80);font-size:12.8px">Thorsten Engler and modified example presented by Sven + my invention ;).</span></div><div class="gmail_extra"><br></div><div class="gmail_extra">Note: "$" prefix means -> invisible for programmer/created by compiler. </div><div class="gmail_extra"><span name="Thorsten Engler" style="font-size:12.8px"><span style="white-space:nowrap"><br></span></span></div><div class="gmail_extra"><div class="gmail_extra"><span style="font-size:12.8px">=== unit1 begin ===</span></div><div class="gmail_extra"><span style="font-size:12.8px">unit unit1;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">interface</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">uses</span></div><div class="gmail_extra"><span style="font-size:12.8px"> InterfaceUsedUnit1,</span></div><div class="gmail_extra"><span style="font-size:12.8px"> InterfaceUsedUnit2;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">//...</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">Procedure $AddRef;</span></div><div class="gmail_extra"><span style="font-size:12.8px">Procedure $Release;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">type</span></div><div class="gmail_extra"><span style="font-size:12.8px"> PSomeType = ^TSomeType;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> TSomeType = record </span></div><div class="gmail_extra"><span style="font-size:12.8px"> ... with management operators</span></div><div class="gmail_extra"><span style="font-size:12.8px"> end;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">function GetSomething: PSomeType;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">implementation</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">uses</span></div><div class="gmail_extra"><span style="font-size:12.8px"> ImplementationUsedUnit;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">var</span></div><div class="gmail_extra"><span style="font-size:12.8px"> BufferForTSomeTypeManagementOperators: PByte;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">procedure $Init;</span></div><div class="gmail_extra"><span style="font-size:12.8px">begin</span></div><div class="gmail_extra"><span style="font-size:12.8px"> InterfaceUsedUnit1.$AddRef;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> InterfaceUsedUnit2.$AddRef;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> ImplementationUsedUnit.$AddRef;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> //.. code from original initialization section goes here</span></div><div class="gmail_extra"><span style="font-size:12.8px">end;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">procedure $Finit;</span></div><div class="gmail_extra"><span style="font-size:12.8px">begin</span></div><div class="gmail_extra"><span style="font-size:12.8px"> //.. code from original finalization section goes here</span></div><div class="gmail_extra"><span style="font-size:12.8px"> ImplementationUsedUnit.$Release;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> InterfaceUsedUnit2.$Release;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> InterfaceUsedUnit1.$Release;</span></div><div class="gmail_extra"><span style="font-size:12.8px">end;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">var</span></div><div class="gmail_extra"><span style="font-size:12.8px"> $RefCount: Integer;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> </span></div><div class="gmail_extra"><span style="font-size:12.8px">var</span></div><div class="gmail_extra"><span style="font-size:12.8px"> gSomething: TSomeType;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> gSomething2: array[0..2] of TSomeType;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> </span></div><div class="gmail_extra"><span style="font-size:12.8px">function GetSomething: PSomeType;</span></div><div class="gmail_extra"><span style="font-size:12.8px">begin</span></div><div class="gmail_extra"><span style="font-size:12.8px"> if not Assigned(gSomething.inst) then</span></div><div class="gmail_extra"><span style="font-size:12.8px"> gSomething.inst := TObject.Create;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> Result := @gSomething;</span></div><div class="gmail_extra"><span style="font-size:12.8px">end;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">procedure $ManagementInit;</span></div><div class="gmail_extra"><span style="font-size:12.8px">begin</span></div><div class="gmail_extra"><span style="font-size:12.8px"> $managementinitialization(); // execute our new section for operators purpose</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px"> // genereted for all global variables with management operators</span></div><div class="gmail_extra"><span style="font-size:12.8px"> InitializeArray(@gSomething, TypeInfo(TSomeType), 1); // call the Initialize operator</span></div><div class="gmail_extra"><span style="font-size:12.8px"> InitializeArray(@gSomething2[0], TypeInfo(TSomeType), 3);</span></div><div class="gmail_extra"><span style="font-size:12.8px">end;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">procedure $ManagementFinit;</span></div><div class="gmail_extra"><span style="font-size:12.8px">begin</span></div><div class="gmail_extra"><span style="font-size:12.8px"> // genereted for all global variables with management operators</span></div><div class="gmail_extra"><span style="font-size:12.8px"> FinalizeArray(@gSomething, TypeInfo(TSomeType), 1); // call the Finalize operator</span></div><div class="gmail_extra"><span style="font-size:12.8px"> FinalizeArray(@gSomething2[0], TypeInfo(TSomeType), 3);</span></div><div class="gmail_extra"><span style="font-size:12.8px"> </span></div><div class="gmail_extra"><span style="font-size:12.8px"> $managementfinalization(); // execute our new section for operators purpose</span></div><div class="gmail_extra"><span style="font-size:12.8px">end;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">procedure $AddRef;</span></div><div class="gmail_extra"><span style="font-size:12.8px">begin</span></div><div class="gmail_extra"><span style="font-size:12.8px"> If LockedInc($RefCount) = 1 then</span></div><div class="gmail_extra"><span style="font-size:12.8px"> begin</span></div><div class="gmail_extra"><span style="font-size:12.8px"> $ManagementInit;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> $Init;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> end;</span></div><div class="gmail_extra"><span style="font-size:12.8px">end;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">procedure $Release;</span></div><div class="gmail_extra"><span style="font-size:12.8px">begin</span></div><div class="gmail_extra"><span style="font-size:12.8px"> If LockedDec($RefCount) = 0 then</span></div><div class="gmail_extra"><span style="font-size:12.8px"> begin</span></div><div class="gmail_extra"><span style="font-size:12.8px"> $Finit;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> $ManagementFinit;</span></div><div class="gmail_extra"><span style="font-size:12.8px"> end;</span></div><div class="gmail_extra"><span style="font-size:12.8px">end;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">managementinitialization // in the fact it is procedure with symbol $managementinitialization </span></div><div class="gmail_extra"><span style="font-size:12.8px"> BufferForInitializeOperator := GetMem(100);</span></div><div class="gmail_extra"><span style="font-size:12.8px">initialization</span></div><div class="gmail_extra"><span style="font-size:12.8px"> $AddRef;</span></div><div class="gmail_extra"><span style="font-size:12.8px">finalization</span></div><div class="gmail_extra"><span style="font-size:12.8px"> $Release;</span></div><div class="gmail_extra"><span style="font-size:12.8px">managementfinalization // in the fact it is procedure with symbol $managementfinalization </span></div><div class="gmail_extra"><span style="font-size:12.8px"> FreeMem(BufferForInitializeOperator);</span></div><div class="gmail_extra"><span style="font-size:12.8px">end.</span></div><div class="gmail_extra"><span style="font-size:12.8px">=== unit1 end ===</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra">simplified than unit1, but behind the scene is the same mechanism</div><div class="gmail_extra"><br></div><div class="gmail_extra"><span style="font-size:12.8px">=== unit2 begin ===</span></div><div class="gmail_extra"><span style="font-size:12.8px">unit unit2;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">interface</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">implementation</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">uses</span></div><div class="gmail_extra"><span style="font-size:12.8px"> unit1;</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><span style="font-size:12.8px">//</span></div><div class="gmail_extra"><span style="font-size:12.8px">initialization </span></div><div class="gmail_extra"><span style="font-size:12.8px"> Writeln(GetSomething^.inst.ToString); // no memory leak anymore even if inst is filled inside TSomeType.Initialize operator</span></div><div class="gmail_extra"><span style="font-size:12.8px">end.</span></div><div class="gmail_extra"><span style="font-size:12.8px">=== unit2 end ===</span></div><div class="gmail_extra"><span style="font-size:12.8px"><br></span></div></div><div class="gmail_extra">-- <br><div><div dir="ltr"><div>Best regards,<br>Maciej Izak</div></div></div>
</div></div>