Hello wp again, "old friend". Thank you very much for your reply.
The problem is the Action := caFree in the OnClose handler of TFormMM. It destroys the form but does not set the formvariable to nil. You close the form in the OnClick handler ABORT button, and, because of caFree, this destroys the form but leaves FormMM non-nil which allows your free_memo to try to destry it again.
I think now I understand the problem. As often I'm impressed how much details you know.
Since everything happens in a single routine, I remove the caFree and rely on the Free_memo alone.
If I understand you correctly, you suggest to remove the 'CloseAction:=caFree' completely. But if I remember correctly, this command was essential neccessary to implement the feature, that if the main program terminates and the dynamic Memo-Form is still open, that this Memo-Form is automatically closed. I use this feature in a couple of programs and don't want to loose it, if possible.
Alternatively the OnClose handler of FormMM must be provided by the calling program which can simultaneously set FormMM to nil.
As rvk wrotes (see below), FormMM:=nil seems not to be neccessary?
...
When using CloseAction := caFree you shouldn't free anything yourself.
So either completely remove the freeandnil or remove the caFree.
There shouldn't be any need for setting FormMM to nil.
You'r not using it anymore after doing that so it has no use.
Thanks a lot again for that infos rvk.
Because I think I need the 'CloseAction:=caFree' (see above), I removed FreeAndNil() and free_Memo() completely and made a test with Heaptrc:
In my little demo I pressed the Start-Button several times and sometimes I pressed ABORT and sometimes not. After the Close-Button Heaptrc showed everything is OK!
But in my
real world program I had heavy fights with Heaptrc:
- after situation 1 (Memo keeps open because of CheckBox, see above) it was
not possible to stop the program with it's Close-Button. From writeln() commands I could see, that the Close-Button-Event and the OnClose-Event of Form1 both were called - but the program did
not terminate. So it was not possible to see the Heaptrc result :-(
- after situations 2+3 (see above) Heaptrc showed "thousands" of lines (more than the console buffer can hold) like:
...
An unhandled exception occurred at $0000000000437117:
EAccessViolation:
$0000000000437117
An unhandled exception occurred at $0000000000437117:
EAccessViolation:
$0000000000437117
An unhandled exception occurred at $0000000000437117:
EAccessViolation:
Speicherzugriffsfehler (Speicherabzug geschrieben)
I tried different Lazarus versions, but no difference. This was on Linux (Ubuntu 18.04).
Then I tried the same Lazarus versions (Laz 1.8.4/FPC 3.0.4 + Laz 2.0.6/FPC 3.0.4) on Windows 7 and then:
- the problem after situation 1 was the same but
- the problem after situations 2+3 did
not occur and Heaptrc showed everything is OK.
Obviously Heaptrc has a problem on Linux, which not exists on Windows... Because this was the very 1st time I used Heaptrc, I searched for documentation, to see if I made something wrong, but in
https://wiki.lazarus.freepascal.org/heaptrc I saw nothing about that.
Question:Has anybody an idea, why Heaptrc fails after situations 2+3 (on Linux only) and after situation 1 (on Linux and Windows)?
I disabled in Project Options / Debugging all 6 checks: -Ci -Cr -Co -Ct -CR -Sa, but this made no difference.
Some of the units I use in my real world program are in {$mode TP} (because they were started decades ago). Could this mix be a reason?
If I could get Heaptrc to work also after situation 1 and see, that everything is OK, then simply removing FreeAndNil() and free_Memo() completely would be a very fine solution :-)
Solution for above Heaptrc-problems (added June 18th 2020):Meanwhile I could solve the 2 problems with Heaptrc:
- it was not possible to stop the program with it's Close-Button, because a loop ran endless when Heaptrc was active, while without Heaptrc it did not. I could fix it.
- the "thousands" of EAccessViolations (on Linux only) occured only, because function system.get_cmdline() was called. Seems to be a bug in FPC 3.0.4, because in FPC 3.2.0-beta rev=43824 this problem does not occur. Sometimes I prefer system.get_cmdline() instead of ParamStr() because the last does several manipulations and can on Windows return different charsets (sometimes Windows-charset and sometimes UTF8).
After Heaptrc now works perfectly, I made a couple of tests with my solution to remove FreeAndNil() in free_Memo() and the results were always "everything ok".