how can i free object memory after returning it in a function?

 

 this code is returning an object to main function but there is memory leak and it gets enormous when its in a loop


void OnStart(){

   trendObject *trend = new trendObject();
   trend = findTrend("EURUSD", PERIOD_D1, 13);

   delete trend;
}

trendObject *findTrend(string symbol, ENUM_TIMEFRAMES _TIMEFRAME_, int pos){
  
   trendObject *trendObj = new trendObject();   //this object makes problem

   return trendObj;

}

2025.11.19 10:28:25.180    testSmall (EURUSD,H1)    1 undeleted dynamic object found:
2025.11.19 10:28:25.180    testSmall (EURUSD,H1)       1 object of class 'trendObject'
2025.11.19 10:28:25.180    testSmall (EURUSD,H1)    128 bytes of leaked memory found

how can i return the object and release memory after function return?

 
appacki:
void OnStart(){

   trendObject *trend = new trendObject();
   trend = findTrend("EURUSD", PERIOD_D1, 13);

   delete trend;
}

trendObject *findTrend(string symbol, ENUM_TIMEFRAMES _TIMEFRAME_, int pos){
  
   trendObject *trendObj = new trendObject();   //this object makes problem

   return trendObj;

}

You created 2 objects and deleted 1.

You deleted an object you created inside the function, but you didn't delete the object you created before calling the function.