Gentlefolk,
Lazarus 0.9.28, ZEOS 6.6.6, Ein XP SP3.
I have the following code:
if FormOmega.FO = NIL then
FormOmega.FO := TFormOmeg.Create(Self);
FormOmega.FO.ShowModal;
FormOmega.FO.Free;
On return from the ShowModal the .Free is executed but does not appear to release the FormOmega.FO.
Next time through FormOmega.Fo is not NIL, the create is skipped and the program crashes on the ShowModal.
From experience, examples, Delphi documentation, etc the above code looks OK.
The code is correct. (except for not setting FormOmega.FO to nil)
What you need to know, is that FormOmega.FO in reality is a pointer.
Pascal hides this fact from you on almost every level. But in some cases you still get to see the behaviour of a pointer.
That means looking at your TFormOmeg object, you have:
1) the object, somewhere in memory (allocation/deallocation is handled automatically)
2) FormOmega.FO a pointer to this memory
3) any other amount of pointers
SomeOtherFO := FormOmega.FO;
the object doesn't know how many pointers point to it, nor where the pointers are.
FormOmega.FO.Free;
frees the object, and deallocates the memory used by it. (but because the object doesn't know the pointer(s), the pointer(s) are still pointing to this (no unused or re-used for something else) memory)
So yes, next time FormOmega.FO still points to somewhere. but the location it points to does not have any usable content.
FreeAndNil(FormOmega.FO); goes one step further. It calls:
FormOmega.FO.Free;
and does
FormOmega.FO := nil;
Note: If you have more pointers to this objects, all other pointers still point to the now invalid memory.