Why release array memory when exiting a function?

 

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?

 
Vladislav Boyko: 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?

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.

 
Fernando Carreiro #:

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

 
I'm not talking about using ArrayFree() in general. I'm only talking about using ArrayFree() on local dynamic arrays before exiting the function. That is, right before this memory is freed automatically. 
 
Vladislav Boyko #:

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.

 
Fernando Carreiro #:

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.

 
Alain Verleyen #:

Of course not. The dynamic array is automatically freed.

The documentation is misleading.

Actually, I am quite sure about that, the compiler checks for the return value of the function and, if necessary, "creates" the required memory on the calling functions stack.

Else, the memory would be on the local stack, which gets freed on return anyways.
 
The documentation states about ArrayFree():

"In case it is necessary to manually manage the memory in complex dynamic environment of the application, ArrayFree() function allows users to free the memory occupied by the already unnecessary dynamic array explicitly and immediately."

So you can free the arrays that have outlived their use immediately if you see the need to free the ram for some reason.
 
Dominik Egert #:

Else, the memory would be on the local stack, which gets freed on return anyways.
OK that's good to know, I would have asked that soon.
Reason: