Suspected Memory Retention

 

I have an issue with a suspected Memory Retention (Bug?) in Container Reuse (CArrayInt). I tried ChatGPT but could not get much from it, but it took on the onus of drafting for me a detailed query that precisely describes the observed behaviour and the concern I'm facing, including relevant background for anyone who would try to reproduce or understand the issue. :)

Context:

In an MQL5 project involving CArrayInt (and sometimes CArrayDouble), I’m dynamically creating new object instances, populating them with data, then deleting them and immediately creating fresh new instances. Logically, I expect a newly created instance to be completely clean—its internal arrays (m_data[]) should be empty and uninitialised.

🔍 Unexpected Behaviour

When I do this:

CArrayInt *index = new CArrayInt();
// populate
...

delete index; // full deletion
index = new CArrayInt(); // recreate

I observe that:

The newly created index retains data that belonged to the previous, now-deleted object.

If I don’t delete the object and simply reassign a new one, everything is fine—it starts clean.

This violates expected memory behaviour, suggesting that either:
  1. MQL5's internal memory reuse is aggressive, ArrayResize(..., 0) or delete
     is failing to fully clean up,
  2. Or there’s a shared pointer or reference held somewhere unexpectedly.

🔧 What I’m Using Internally

I’m not calling  .Clear() explicitly.

The Shutdown() method on CArrayInt uses:

if(ArrayResize(m_data, 0) == -1) return false;
m_data_total = 0;
m_data_max = 0;

This should clear the internal m_data[] array, but either:

  1. It’s not getting called during delete, or
  2. ArrayResize(..., 0) is not zeroing the array in memory.

❓ My Questions / Research Focus
  1. Why does a newly created CArrayInt instance hold on to old data only if the previous one was deleted?
  2. Is ArrayResize(..., 0) or delete truly equivalent to fresh memory in MQL5?
  3. What internal memory model does MQL5 use for new and delete? Does it zero memory or just mark it as available?
  4. Could memory be reused because of uncollected references, and if so, how to force full object disposal?
  5. Is there a RAII-style design workaround in MQL5, considering the lack of true smart pointers?

💡 What I Suspect

The delete operator does correctly trigger the destructor of CArrayInt, and the destructor in turn calls Shutdown(). However, the m_data[] array inside CArrayInt may still hold content because:

  1. A hidden reference to m_data[] might still exist somewhere in the program (e.g. inside another object like CArrayObj), preventing full cleanup or leading to shared state.
  2. Alternatively, MQL5's memory release for arrays and objects—especially those allocated dynamically—may not immediately zero or reinitialise memory, even though the destructor runs. This could cause the next new allocation to reuse the same memory block with stale array content still intact.
Does anyone have further suggestions on how to deal with this issue?