You misunderstand! It is recommending that use dynamically allocated memory instead of stack memory (which is what normal local variable consume), when using variables that consume a lot of memory.
And for that case, when allocating dynamic arrays, you would use ArrayFree() to release it, just as you would use delete to release a dynamically allocated class object.
Remember that there is also the case where you want to pass out a reference/pointer to the dynamically allocated data, when the function returns, so automated release is not always desired.
Even if the underlying system does do clean-up or garbage collection, it is always a good practice to release memory explicitly to prevent acidental memory leaks.
You misunderstand! It is recommending that use dynamically allocated memory instead of stack memory (which is what normal local variable consume), when using variables that consume a lot of memory.
And for that case, when allocating dynamic arrays, you would use ArrayFree() to release it, just as you would use delete to release a dynamically allocated class object.
Thank you, Fernando.
But I still don’t understand why the memory occupied by a local dynamic array should be freed (using ArrayFree) before exiting the function.
void OnStart() { printMemoryUsed("Before calling the function"); f(); printMemoryUsed("After exiting the function"); } void f() { double arr[]; ArrayResize(arr, 10000000); printMemoryUsed("Before exiting the function"); } void printMemoryUsed(string prefix) { Print(prefix, ": ", MQLInfoInteger(MQL_MEMORY_USED)); }
Before calling the function: 0 Before exiting the function: 77 After exiting the function: 0
Wouldn't it be a terminal bug if the memory occupied by a local dynamic array is not freed after the function exits?
As for me, this is the same as adding the ability to manually (using delete) delete objects with the POINTER_DYNAMIC pointer type. I mean, it's just as meaningless
Thank you, Fernando.
But I still don’t understand why the memory occupied by a local dynamic array should be freed (using ArrayFree) before exiting the function.
Wouldn't it be a terminal bug if the memory occupied by a local dynamic array is not freed after the function exits?
As for me, this is the same as adding the ability to manually (using delete) delete objects with the POINTER_DYNAMIC pointer type. I mean, it's just as meaningless
I think we may be discussing different things here.
The "local" variable that holds the "reference" will be released, but not the "dynamically" allocated data for it, because it can be passed out of the function to be used elsewhere.
So, it needs to be explicitly released before exiting the function, or else it will be "leaked" memory.
I think we may be discussing different things here.
The "local" variable that holds the "reference" will be released, but not the "dynamically" allocated data for it, because it can be passed out of the function to be used elsewhere.
So, it needs to be explicitly released before exiting the function, or else it will be "leaked" memory.
Of course not. The local dynamic array is automatically freed.
The documentation is misleading.
Of course not. The dynamic array is automatically freed.
The documentation is misleading.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Stack #
Therefore, for large local data you should better use dynamic memory - when entering a function, allocate the memory, which is required for local needs, in the system (new, ArrayResize()), and when exiting the function, release the memory (delete, ArrayFree()).Why does the documentation suggest calling ArrayFree() when exiting a function? Wouldn't the memory occupied by the array be automatically released when the function exits?