Hi,
The reason is first time it fail because Self is nil (not defined), and that because of this global var
Initially it is nil (zeroed), after creating it once, it will held a valid value and full MienForm structure/object, BUT after freeing it, Delphi Memory Manager (FastMM) most likely will save time and not clear the allocated memory hence the structure of the TMainForm is reserved and pointing to a valid
VMT, also you are free the global without nil (zeroing) hence it still point to a valid to read but not valid to be trusted pointer to memory/data/object/class/structure...
If you enable Memory Debugging tools then it will point that this is case of use after free, i mean the second call.
Also you can simply nil that var and get failure on the second time, a consistent failure as it should be.
Also this is hidden and dangerous bug as accessing Self second time could lead to undefined behavior as the memory from MM point of view is freed and empty and can be reused, this can lead to all sort of unexplained
exception and no sense stack traces....
The simplest way to see this
exception and protect against it is
1) Don't use global var !
2) In this case im my opinion is a legitimate place to use FreeAndNil(MeinForm); instead of MeinForm.Free;
3) Before calling MeinForm := TMeinForm.Create(Application); you really should check for Assigned(MeinForm) then raise an
exception, that Create should only happen if MeinForm already created to prevent memory corruption.
..