In the current problem there is no leak shown by HeapTrc. I have done some research on using Xcode Instruments to profile my memory usage and how to install Valgrind on MacOs.
You may not have a "classical leak" in the sense that the leaked memory is unreachable.
That is, usually when thinking of a leak, you think of
a := alloc_some_mem; // could be an object created or anything direct memalloc, or....
a := nil; // you did not free it, but you no longer have a variable to reach it, so you can no longer free it
But of course you could have
var a: array of TFoo;
begin
SetLength(a, length(a) + 1);
a[length(a)-1] := TFoo.create;
You keep the pointers forever, you just never free them, until the app exits (so HeapTrc says they all got freed).
If you run that long enough, you run out of mem.
The above is of course simplified. But a linked list would be less obvious.
Or:
The button is hold by the owner Form1. You may never destroy it yourself, the form will, but only once the form is destroyed, which may only be when the app exits. (Hide/Show does not destroy it).
A complete other scenario to run out of memory is, to not run out of mem, but not to have any large enough block: fragmentation.
If your app runs for a long long time, and does frequent alloc and free. Imagine you memory as continuous list of bytes.
- Alloc 10 from 0 to 9
- Alloc 10 from 10 to 19
- Free the first 10
- Alloc 11 => you can't use the free block of 10 at the start, you need to get fresh mem.
Imagine you got all the memory fragmented, so the largest block is maybe 1000, but you need 1100.
You can get the fpc heapstatus, and query (and writeln for debugging) the amount of really allocated mem. Then you see if that goes up, or not.
But keep in mind, fragmentation is not overly common. It does happen, but its on the lower likelihood of being your problem.
Though, then again, some server apps are written to alloc mem once at start up, and then never alloc or free any after that, and at least in part to avoid fragmentation.