From 507375764d675427a11fa658eb17d201f3bf773a Mon Sep 17 00:00:00 2001 From: narfunikita Date: Thu, 7 May 2015 13:38:17 +0300 Subject: [PATCH] Fix csharp cscvrptw example. Protect callbacks from GC. --- examples/csharp/cscvrptw.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/examples/csharp/cscvrptw.cs b/examples/csharp/cscvrptw.cs index a583d86444..b3378812f4 100644 --- a/examples/csharp/cscvrptw.cs +++ b/examples/csharp/cscvrptw.cs @@ -241,10 +241,12 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows { model.AddDimension(demand_callback, 0, vehicle_capacity_, true, "capacity"); // Setting up vehicles + NodeEvaluator2[] cost_callbacks = new NodeEvaluator2[number_of_vehicles]; for (int vehicle = 0; vehicle < number_of_vehicles; ++vehicle) { int cost_coefficient = vehicle_cost_coefficients_[vehicle]; NodeEvaluator2 manhattan_cost_callback = new Manhattan(locations_, cost_coefficient); + cost_callbacks[vehicle] = manhattan_cost_callback; model.SetVehicleCost(vehicle, manhattan_cost_callback); model.CumulVar(model.End(vehicle), "time").SetMax( vehicle_end_time_[vehicle]); @@ -267,6 +269,13 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows { Console.WriteLine("Search"); Assignment solution = model.SolveWithParameters(parameters, null); + //protect callbacks from the GC + GC.KeepAlive(manhattan_callback); + GC.KeepAlive(demand_callback); + for (int cost_callback_index = 0; cost_callback_index < cost_callbacks.Length; cost_callback_index++) { + GC.KeepAlive(cost_callbacks[cost_callback_index]); + } + if (solution != null) { String output = "Total cost: " + solution.ObjectiveValue() + "\n"; // Dropped orders