Download MetaTrader 5
To add comments, please log in or register
Do you keep a blog? Place a link to MQL5.community there and make money!
Tom Clayson
215
Tom Clayson 2014.12.05 16:33 

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);
}
Enrico Lambino
4280
Enrico Lambino 2014.12.05 17:59  
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.

Dr.Trader
3752
Dr.Trader 2014.12.05 18:38  

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 :) 

Alain Verleyen
Moderator
30298
Alain Verleyen 2014.12.05 19:30  
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().

Tom Clayson
215
Tom Clayson 2014.12.06 09:24  
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.
Alain Verleyen
Moderator
30298
Alain Verleyen 2014.12.06 12:17  
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.

/
To add comments, please log in or register