Search code examples
mql5

CHashMap memory not being released on class deinitialisation


I have a class as below (simplified for the post):

class Person
{
public:
   Person();
   ~Person();

private:
   CHashMap<string, int> Contacts;
}

Now, when I deinitialise my EA, I get some logs saying:

X undeleted objects left
1 object of type CHashMap<string, int> left
.
.
.
X bytes of leaked memory

In my EA I declare as:

Person p;

And in the OnDeinit function I am calling:

void OnDeinit(const int reason)
{
  EventKillTimer();
  
  // clean objects
  delete GetPointer(p);
}

And in the class deinitialisation function I am doing the below:

Person::~Person()
{
   delete GetPointer(Contacts);
}

But still memory leaks... Any help as to why this is the case?


Solution

  • It seems the memory leak was down to the fact that I was not using classes properly. The Person class should be declared as a pointer, and so, when calling the delete operator it clears the memory.

    So, instead now :

    Person *p;

    And then :

    void OnDeinit(const int reason)
    {
      EventKillTimer();
      
      // clean objects
      delete p;
    }
    

    The issue is not with the class itself (i.e. the deinitialisation function) as that seems to clear itself fine.