Page 1 of 1
Protected NewList
Posted: Mon Jul 01, 2024 11:12 am
by sst
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:
Code: Select all
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
Re: Protected NewList
Posted: Mon Jul 01, 2024 11:27 am
by BarryG
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"?
Arrays:
Code: Select all
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.
Lists:
Code: Select all
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.
Re: Protected NewList
Posted: Mon Jul 01, 2024 3:02 pm
by jacdelad
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.
Re: Protected NewList
Posted: Mon Jul 01, 2024 5:06 pm
by PBJim
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.