Problem when trying to deallocate RAM use by pointers of my EA

 

Hello people! :D

All right?

I have a question to solve about the use of pointers.

I created this EA attached and I can not deallocate the use of RAM memory by the pointers present in EA. I already used delete on pointers and arrays and even then the RAM remains allocated just increasing the usage as the EA backtest passes. I read all the pointer documentation to see what was missing and I still haven't found the solution.

Can anyone give me a help of what I am wrong and/or what I can improve to be able to release the memory accumulated by the pointers of the EA?

Thanks in advance for your attention and help! :D

Files:
 
  1. CArrayObj *ArrayPositionsBuy = new CArrayObj;
    CArrayObj *ArrayPositionsSell = new CArrayObj;
    1. Don't do that. Allocate it on OnInit and deallocate in OnDeinit.

      That is not an assignment; it's initialization of a common (globally declared,) or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

      1. They are initialized once on program load.

      2. They don't update unless you assign to them.

      3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

        MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

        Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

        1. Terminal starts.
        2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
        3. OnInit is called.
        4. For indicators OnCalculate is called with any existing history.
        5. Human may have to enter password, connection to server begins.
        6. New history is received, OnCalculate called again.
        7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    2. You've allocated it once (on load) and tried to deallocate it on each deinit/init cycle (change of timeframe, etc.) Only the first deallocate works.
    3. Why are you using pointers there at all?  Make them the actual object and clear it on deinit.

  2.       else
            {
             ArrayPositionsBuy = new CArrayObj();
            }
    
    You just lost your original pointer and now have a memory leak.
 
William Roeder:

William, you are the best!

Thank you very much for your excellent answer!

You helped me a lot with these excellent and essential information / concepts that you shared, I am starting to use pointers in practice, so I am learning a lot.

I made the corrections and used your tips ...

I removed the new in the global declaration of the CArrayObj pointers, I removed the internal delete from the global pointers that I was desperately using to try to deallocate the memory, and finally, I found another memory accumulator that I found in the functions that update the profit of the buy and sell positions in the array, I replaced the global pointer with a local that I could delete it when it didn’t need to be added to the vector (in situations where I just needed to update the profit already present in the array), so I managed to deallocate  this memory that I couldn’t by the fact that I was using a global pointer.

Thank you so much, thank you so much, thank you so much for your brilliant help!

It solved my pointer problems for a long time! Until I advance further in new knowledge.

You are the best!

Thank you!


If anyone wishes, the code with the corrections is attached.

Files:
Reason: