Skip to content

Commit

Permalink
fixing memory leak and refactoring #14034 #12
Browse files Browse the repository at this point in the history
  • Loading branch information
behrisch committed Nov 10, 2023
1 parent 3796241 commit ea27a5d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 51 deletions.
57 changes: 27 additions & 30 deletions src/router/ROPerson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ ROPerson::PersonTrip::getDuration() const {

bool
ROPerson::computeIntermodal(SUMOTime time, const RORouterProvider& provider,
PersonTrip* const trip, const ROVehicle* const veh, MsgHandler* const errorHandler) {
const PersonTrip* const trip, const ROVehicle* const veh,
std::vector<TripItem*>& resultItems, MsgHandler* const errorHandler) {
const double speed = getMaxSpeed() * trip->getWalkFactor();
std::vector<ROIntermodalRouter::TripItem> result;
provider.getIntermodalRouter().compute(trip->getOrigin(), trip->getDestination(),
Expand Down Expand Up @@ -358,9 +359,9 @@ ROPerson::computeIntermodal(SUMOTime time, const RORouterProvider& provider,
}
}
if (&item == &result.back() && trip->getStopDest() == "") {
trip->addTripItem(new Walk(start, item.edges, item.cost, item.exitTimes, depPos, arrPos));
resultItems.push_back(new Walk(start, item.edges, item.cost, item.exitTimes, depPos, arrPos));
} else {
trip->addTripItem(new Walk(start, item.edges, item.cost, item.exitTimes, depPos, arrPos, item.destStop));
resultItems.push_back(new Walk(start, item.edges, item.cost, item.exitTimes, depPos, arrPos, item.destStop));
}
} else if (veh != nullptr && item.line == veh->getID()) {
double cost = item.cost;
Expand All @@ -369,16 +370,16 @@ ROPerson::computeIntermodal(SUMOTime time, const RORouterProvider& provider,
route->setProbability(1);
veh->getRouteDefinition()->addLoadedAlternative(route);
carUsed = true;
} else if (trip->needsRouting()) {
} else if (resultItems.empty()) {
// if this is the first plan item the initial taxi waiting time wasn't added yet
const double taxiWait = STEPS2TIME(string2time(OptionsCont::getOptions().getString("persontrip.taxi.waiting-time")));
cost += taxiWait;
}
trip->addTripItem(new Ride(start, item.edges.front(), item.edges.back(), veh->getID(), trip->getGroup(), cost, item.arrivalPos, item.length, item.destStop));
resultItems.push_back(new Ride(start, item.edges.front(), item.edges.back(), veh->getID(), trip->getGroup(), cost, item.arrivalPos, item.length, item.destStop));
} else {
// write origin for first element of the plan
const ROEdge* origin = trip == myPlan.front() && trip->needsRouting() ? trip->getOrigin() : nullptr;
trip->addTripItem(new Ride(start, origin, nullptr, item.line, trip->getGroup(), item.cost, item.arrivalPos, item.length, item.destStop, item.intended, TIME2STEPS(item.depart)));
const ROEdge* origin = trip == myPlan.front() && resultItems.empty() ? trip->getOrigin() : nullptr;
resultItems.push_back(new Ride(start, origin, nullptr, item.line, trip->getGroup(), item.cost, item.arrivalPos, item.length, item.destStop, item.intended, TIME2STEPS(item.depart)));
}
}
start += TIME2STEPS(item.cost);
Expand All @@ -399,36 +400,32 @@ ROPerson::computeRoute(const RORouterProvider& provider,
for (PlanItem* const it : myPlan) {
if (it->needsRouting()) {
PersonTrip* trip = static_cast<PersonTrip*>(it);
std::vector<ROVehicle*>& vehicles = trip->getVehicles();
const std::vector<ROVehicle*>& vehicles = trip->getVehicles();
std::vector<TripItem*> resultItems;
std::vector<TripItem*> best;
const ROVehicle* bestVeh = nullptr;
if (vehicles.empty()) {
computeIntermodal(time, provider, trip, nullptr, errorHandler);
computeIntermodal(time, provider, trip, nullptr, best, errorHandler);
} else {
double bestCost = std::numeric_limits<double>::infinity();
PersonTrip* best = nullptr;
ROVehicle* bestVeh = nullptr;
for (std::vector<ROVehicle*>::iterator v = vehicles.begin(); v != vehicles.end();) {
if (!computeIntermodal(time, provider, trip, *v, errorHandler)) {
delete (*v)->getRouteDefinition();
delete *v;
v = vehicles.erase(v);
} else {
const double cost = trip->getCost();
if (cost < bestCost) {
bestCost = cost;
if (best != nullptr) {
delete best;
}
best = static_cast<PersonTrip*>(trip->clone());
bestVeh = *v;
for (const ROVehicle* const v : vehicles) {
const bool carUsed = computeIntermodal(time, provider, trip, v, resultItems, errorHandler);
double cost = 0.;
for (TripItem* const it : resultItems) {
cost += it->getCost();
}
if (cost < bestCost) {
bestCost = cost;
while (!best.empty()) {
delete best.back();
best.pop_back();
}
trip->clearItems();
++v;
best.swap(resultItems);
bestVeh = carUsed ? v : nullptr;
}
}
if (best != nullptr) {
trip->copyItems(best, bestVeh);
}
}
trip->setItems(best, bestVeh);
}
time += it->getDuration();
}
Expand Down
26 changes: 5 additions & 21 deletions src/router/ROPerson.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,26 +365,9 @@ class ROPerson : public RORoutable {
return myTripItems.empty();
}

double getCost() const {
double result = 0;
for (TripItem* const it : myTripItems) {
result += it->getCost();
}
return result;
}

void clearItems() {
for (TripItem* const it : myTripItems) {
delete it;
}
myTripItems.clear();
}

void copyItems(PersonTrip* trip, ROVehicle* veh) {
for (TripItem* const it : myTripItems) {
delete it;
}
myTripItems = trip->myTripItems;
void setItems(std::vector<TripItem*>& newItems, const ROVehicle* const veh) {
assert(myTripItems.empty());
myTripItems.swap(newItems);
for (auto it = myVehicles.begin(); it != myVehicles.end();) {
if (*it != veh) {
delete (*it)->getRouteDefinition();
Expand Down Expand Up @@ -460,7 +443,8 @@ class ROPerson : public RORoutable {

private:
bool computeIntermodal(SUMOTime time, const RORouterProvider& provider,
PersonTrip* const trip, const ROVehicle* const veh, MsgHandler* const errorHandler);
const PersonTrip* const trip, const ROVehicle* const veh,
std::vector<TripItem*>& resultItems, MsgHandler* const errorHandler);

private:
/**
Expand Down

0 comments on commit ea27a5d

Please sign in to comment.