What to free after CalculateRoute to release allocated memory.

In a logistic application we use TTMSFNCRouteCalculator CalculateRoute to get Driving time and distance.
This perfectly works the only thing we have with it is that after a certain time our application runs out of Memory.
Restarting the application solves the problem.
I create only one fRouteCalculator:= TTMSFNCRouteCalculator.Create; at program start.
What or how do we need to free or clear in order to avoid memory overrun?
Thanks, and Regards
Henry

Hi,

We are not aware of any memory leaks in TTMSFNCRouteCalculator so it should be sufficient to free your FRouteCalculator when the program is closed.
Can you please provide the following information so I can further investigate this?

  • Which version of Delphi are you using?
  • Do you see any memory leaks with ReportMemoryLeaksOnShutdown set to true in your application? If so, are the leak references related to TTMSFNCRouteCalculator or not?
  • Can the issue be reproduced in separate application that only contains the TTMSFNCRouteCalculator component?

Dear Bart,
thanks for your replay, indeed my issue is not the classical memory leak. I assume there is a possibility to free the calculated route once I got the distance and Duration.
See line 116.
I created a small sample application to illustrate the issue.
Our Application runs all day and will calculate hundred of destinations. After a certain time we will get out of memory error and need to restart the application. This will free all and we can do our calculation again.
In the attached sample we need to set the number of run to 10 to fill up the memory on our computer.
The attached sample application calculates several location repeatedly to illustrate the issue. In the real application will calculate different position for each run.
Hoppe this shows my Problem.
Thanks and regards
Attachement:
LogistiqueIssue.zip (4.4 KB)

I have been able to reproduce the issue with the sample project you provided.
When performing a high number of route calculations you can remove the data from these requests by doing a Clear on the Directions, Geocoding and Routes collections. This should avoid the Out Of Memory error.

Example

procedure TForm1.TMSFNCRouteCalculator1CalculateRoute(Sender: TObject;
  const ARoute: TTMSFNCRouteCalculatorRoute);
begin
  //...
  fRouteCalculator.Directions.DirectionsRequests.Clear;
  fRouteCalculator.Geocoding.GeocodingRequests.Clear;
  fRouteCalculator.Routes.Clear;
end;

Please note that in this scenario you can reduce the number of executed API requests by using the TTMSFNCDirections component (also part of TMS FNC Maps) instead.
The TTMSFNCRouteCalculator will first execute two requests to geocode the start and end address and then a directions request, based on the previously geocoded coordinates, to calculate the route. With TTMSFNCDirections only a single directions request is executed taking the start and end address as parameters. (This functionality is only available when using the Google API service)

Example:

procedure TForm1.btnCalcRouteClick(Sender: TObject);
begin
   //...
  //        fRouteCalculator.CalculateRoute(sStartAdr, sDestAdr, nil);
            TMSFNCDirections1.GetDirections(sStartAdr, sDestAdr);
  //...
end;

procedure TForm1.TMSFNCDirections1GetDirectionsResult(Sender: TObject;
  const AResult: TTMSFNCDirectionsRequest);
begin
  //...
  aDistance:= AResult.Items[0].Distance;
  aDuration:= AResult.Items[0].Duration;
  TMSFNCDirections1.DirectionsRequests.Clear;
end;
1 Like