Method returns an invalid pointer?!

 

I have the below method, but it returns an invalid pointer, wham am I doing wrong?

RunningOrder _runningOrders[];

RunningOrder* RunningOrdersHelper::LowestBuyOrder() {  

   RunningOrder *result = NULL;   

   for(int i = 0; i < ArraySize(_runningOrders); i++) {

      RunningOrder current = _runningOrders[i];

      if (current.Type != OP_BUY) continue;      

      if (result == NULL) {

         result = GetPointer(current);

      }

      else if (current.OpenPrice < result.OpenPrice) result = GetPointer(current);      

   }

   return result;

}



   RunningOrder *lbo = LowestBuyOrder();   

  if (CheckPointer(lbo)== POINTER_INVALID ) {   

   Print("Invalid Pointer ");   

    } 
Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
The executing subsystem of the client terminal has an opportunity to save the error code in case it occurs during a MQL5 program run. There is a predefined variable _LastError for each executable MQL5 program. Before starting the OnInit function, the _LastError variable is reset...
 
Pooya Khamooshi: wham am I doing wrong?
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. You copy an object in _runningOrders[] and name it current.  You create a pointer of current named result. You return result. On return all objects in the function are released including current, so the pointer becomes bogus.
  3. Simplify, return an index to _runningOrders[] or EMPTY.
 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. You copy an object in _runningOrders[] and name it current.  You create a pointer of current named result. You return result. On return all objects in the function are released including current, so the pointer becomes bogus.
  3. Simplify, return an index to _runningOrders[] or EMPTY.

Thanks William, I've edited.

does this line really make a copy of the object?! I just wanted a variable pointing to the same object.

RunningOrder current = _runningOrders[i];

Maybe I should use the pointer but I tried and it gives me the "invalid pointer access":

RunningOrder *current = _runningOrders[i];

Good idea, I will return the index but just that I needed to return the actual object that is found from this method, not copying it or anything. How'd you return the object which was found then?

 

Ok, this is the solution and it works fine :), just getting a pointer to the object in the array and returning it like C#.


RunningOrder* RunningOrdersHelper::LowestBuyOrder() {  
   RunningOrder *result = NULL;
   
   for(int i = 0; i < ArraySize(_runningOrders); i++) {
      RunningOrder *current = GetPointer(_runningOrders[i]);
      if (current.Type != OP_BUY) continue;
      
      if (result == NULL) result = current;
      else if (current.OpenPrice < result.OpenPrice) result = current;      
   }
   
   return result;
}
Reason: