xxx bytes of undeleted memory when returning an object pointer

 

I needed ti return a collection of results from a function, and so in the absence of tuples I chose to return a CArrayInt object pointer. As I am returning it though, I cannot delete it before doing so, and so end up with the xxx bytes of undeleted memory message. What is the solution to this?


CArrayInt* Highs(const double &Array[], int Start, int Count) {
   CArrayInt* Results = new CArrayInt;
   double Highest = DBL_MAX*-1;
   int Highest_Shift = -1;
   int i;
   for(i=Start;i<=Start+Count;i++) {
      
      if(Array[i]>Highest) {
         Highest = Array[i];
         Highest_Shift = i;         
      }
   }
   
   Results.Add(Highest_Shift);
   // now add any other points of equivalent price
   for(i=Start;i<=Start+Count;i++) {
      if(Array[i]==Highest && i!=Results[0]) {
         Results.Add(i);
      }
   }
   return(Results);
}
 
whitebloodcell:

I needed ti return a collection of results from a function, and so in the absence of tuples I chose to return a CArrayInt object pointer. As I am returning it though, I cannot delete it before doing so, and so end up with the xxx bytes of undeleted memory message. What is the solution to this?


The function returns the object itself, so it cannot be deleted inside. You will need to delete the pointer somewhere outside the function.

 

I am no good in MQL, but I know C++.

It looks similar to c++ code, usual approach would be to pass  Results array as reference function parameter too, like this:

void Highs(const double &Array[], int Start, int Count, CArrayInt &Results) {
   // CArrayInt* Results = new CArrayInt; //no need to create it
   Results.clear(); //or whatewer else you should call to clear any existing entries
   double Highest = DBL_MAX*-1;
   int Highest_Shift = -1;
   int i;
   for(i=Start;i<=Start+Count;i++) {
      
      if(Array[i]>Highest) {
         Highest = Array[i];
         Highest_Shift = i;         
      }
   }
   
   Results.Add(Highest_Shift);
   // now add any other points of equivalent price
   for(i=Start;i<=Start+Count;i++) {
      if(Array[i]==Highest && i!=Results[0]) {
         Results.Add(i);
      }
   }
  
//return(Results); //this no longer needed, but maybe you need to call "return void;" or something like that in mql.
}

 Then just create this  Results array before calling this function, pass it as parameter, then process and delete it. I hope this works in mql :) 

 
whitebloodcell:

I needed ti return a collection of results from a function, and so in the absence of tuples I chose to return a CArrayInt object pointer. As I am returning it though, I cannot delete it before doing so, and so end up with the xxx bytes of undeleted memory message. What is the solution to this?


Something like this in the calling function :

   CArrayInt* result;
   
   ...
   result=Highs(Array,0,10);
   
   ...
   delete result;

If result is a global variable, you can place the delete statement in OnDeinit().

 
Iceron:

The function returns the object itself, so it cannot be deleted inside. You will need to delete the pointer somewhere outside the function.

I declare a Results pointer in the calling function and I do delete it there, but the function returns a copy of the pointer does it not? So there are two pointers, one in the calling function, and one in the function itself. I think Dr.Trader's method will work best and solve the problem, I will try it now.
 
whitebloodcell:
I declare a Results pointer in the calling function and I do delete it there, but the function returns a copy of the pointer does it not? So there are two pointers, one in the calling function, and one in the function itself. I think Dr.Trader's method will work best and solve the problem, I will try it now.

No, there is not 2 pointers. "Results" in Highs() function is a local variable which don't exist anymore once this function is executed.