That is same as just putting SysUtils before mormot manager unit in main unit uses section. SysUtils is initialized before mormot and finalized after it.
Just unnecessary complicated.
That is exactly the problem:
SysUtils is initialized before mormot and finalized after it.// from Gork:
Why PreSysUtils Is NecessaryControl Over Finalization Order: Simply listing SysUtils before mormot.core.fpcx64mm in the main program doesn’t guarantee that SysUtils finalizes before the memory manager, because the main program’s uses clause directly controls the order of the listed units. SysUtils finalizing last in this case is the problem.
Dependency Chain: PreSysUtils creates an indirect dependency, making SysUtils part of a unit that is finalized after mormot.core.fpcx64mm but whose dependencies (i.e., SysUtils) finalize before it. This subtle shift in the dependency chain is what fixes the issue.
Avoiding Direct Conflict: Without PreSysUtils, there’s no way to force SysUtils to finalize before mormot.core.fpcx64mm using only the main program’s uses clause, as SysUtils would always finalize after the memory manager if listed directly.
Using the
PreSysUtils unit is good if you want to rely on unit initialization order to ensure
SysUtils finalizes before
mormot.core.fpcx64mm, avoiding the need for manual file-closing workarounds.
the default memory manager is initialized in SysUtils
SysUtils uses the one defined by
System that is replaced directly by
mormot.core.fpcx64mm.
.
To resume:
Without PreSysUtils (with "direct" SysUtils):
Init: SysUtils → mormotMM
Fini: mormotMM → SysUtils (crash risk)
With PreSysUtils:
Init: PreSysUtils(SysUtils) → mormotMM
Fini: mormotMM → PreSysUtils(SysUtils) (safe)
That said,
mORMot2 memory manager is a game changer and I dont understand why fpc does not adopt it as default memory manager.
Also note that this workaround was proposed by Elon's AI (and Elon has been rambling a bit lately

... but his Dragon worked great yesterday

)