There are cases where a variable of type List is used only within a procedure (and declared as Protected).
In order to avoid “memory leak”, is it necessary to use the FreeList() command before procedure exit?
The same question in the case of a variable of type Map or Array.
A simple example:
Procedure foo()
Protected NewList a.i ()
;Do something with a()
FreeList (a()) ; Is this instruction necessary to avoid memory leak or it is useless?
ProcedureReturn
EndProcedure
I only tested Lists and Arrays, and yes, they're freed when the procedure exits. Proof is below. Run each test and look at the mem usage in Task Manager. Then press the P key to exit the procedure, and the mem usage in Task Manager goes down. Then press E to exit the app.
@Fred: This info would be useful in the manual under "Procedure"?
Procedure test()
Dim a$(99999999)
Repeat : Sleep_(1) : Until GetAsyncKeyState_(#VK_P)<>0 ; Press P to exit this procedure.
EndProcedure
test()
Repeat : Sleep_(1) : Until GetAsyncKeyState_(#VK_E)<>0 ; Press E to exit app.
Procedure test()
Protected NewList a.i()
For n=1 To 9999999
AddElement(a())
a()=n
Next
Repeat : Sleep_(1) : Until GetAsyncKeyState_(#VK_P)<>0 ; Press P to exit this procedure.
EndProcedure
test()
Repeat : Sleep_(1) : Until GetAsyncKeyState_(#VK_E)<>0 ; Press E to exit app.
They are freed automatically, like BarryG said. One thing to consider is that it may take a while to free them if they are very huge. I had the problem, that a procedure took a long time stopping at returning to the main program. I found out, that releasing the very huge list was the culprit. Solved it with a global list and asynchronous cleanup.
BarryG wrote: Mon Jul 01, 2024 11:27 am
@Fred: This info would be useful in the manual under "Procedure"?
It would indeed be good to see this type of broad technical information within PureBasic's documentation. It's precisely the sort of thing that developers more or less know the answer to already, but having it confirmed in the documentation would be so much better.