From 247ce60d9c811ef245d753e081e06102fa0fb40a Mon Sep 17 00:00:00 2001 From: Mikhail Gorbushin Date: Mon, 12 Aug 2019 16:51:22 +0300 Subject: [PATCH] [routing] Fix crash in routing in IndexGraph::IsRestricted Added DropAStarParents method for AStarGraph, because after AStarAlgorithm ended, IndexGraph still links with AStar's parents which are have been already deallocated. --- generator/routing_index_generator.cpp | 19 ++++++++++++------- routing/base/astar_algorithm.hpp | 21 ++++++++++++++++++--- routing/base/astar_graph.hpp | 4 ++++ routing/index_graph_starter.hpp | 5 +++++ routing/index_graph_starter_joints.hpp | 5 +++++ routing/single_vehicle_world_graph.cpp | 9 +++++++++ routing/single_vehicle_world_graph.hpp | 1 + routing/world_graph.cpp | 1 + routing/world_graph.hpp | 1 + 9 files changed, 56 insertions(+), 10 deletions(-) diff --git a/generator/routing_index_generator.cpp b/generator/routing_index_generator.cpp index e0177c6b94..5473605688 100644 --- a/generator/routing_index_generator.cpp +++ b/generator/routing_index_generator.cpp @@ -172,6 +172,7 @@ public: CHECK(false, ("This method exists only for compatibility with IndexGraphStarterJoints")); return GetAStarWeightZero(); } + bool AreWavesConnectible(map const & /* forwardParents */, JointSegment const & /* commonVertex */, map const & /* backwardParents */, @@ -179,6 +180,16 @@ public: { return true; } + + void SetAStarParents(bool /* forward */, map & parents) + { + m_AStarParents = &parents; + } + + void DropAStarParents() + { + m_AStarParents = nullptr; + } // @} m2::PointD const & GetPoint(Segment const & s, bool forward) @@ -186,11 +197,6 @@ public: return m_graph.GetPoint(s, forward); } - void SetAStarParents(bool /* forward */, map & parents) - { - m_AStarParents = &parents; - } - void GetEdgesList(Segment const & child, bool isOutgoing, vector & edges) { m_graph.GetEdgeList(child, isOutgoing, true /* useRoutingOptions */, edges); @@ -473,8 +479,7 @@ void FillWeights(string const & path, string const & mwmFile, string const & cou Algorithm astar; IndexGraphWrapper indexGraphWrapper(graph, enter); DijkstraWrapperJoints wrapper(indexGraphWrapper, enter); - AStarAlgorithm::Context context; - indexGraphWrapper.SetAStarParents(true /* forward */, context.GetParents()); + AStarAlgorithm::Context context(wrapper); unordered_map> visitedVertexes; astar.PropagateWave(wrapper, wrapper.GetStartJoint(), [&](JointSegment const & vertex) diff --git a/routing/base/astar_algorithm.hpp b/routing/base/astar_algorithm.hpp index 08e050fee1..287325f72f 100644 --- a/routing/base/astar_algorithm.hpp +++ b/routing/base/astar_algorithm.hpp @@ -112,6 +112,16 @@ public: class Context final { public: + Context(Graph & graph) : m_graph(graph) + { + m_graph.SetAStarParents(true /* forward */, m_parents); + } + + ~Context() + { + m_graph.DropAStarParents(); + } + void Clear() { m_distanceMap.clear(); @@ -159,6 +169,7 @@ public: void ReconstructPath(Vertex const & v, std::vector & path) const; private: + Graph & m_graph; std::map m_distanceMap; std::map m_parents; }; @@ -243,6 +254,11 @@ private: graph.SetAStarParents(forward, parent); } + ~BidirectionalStepContext() + { + graph.DropAStarParents(); + } + Weight TopDistance() const { ASSERT(!queue.empty(), ()); @@ -399,7 +415,7 @@ AStarAlgorithm::FindPath(P & params, RoutingResult::Result remainingDistance += it->GetWeight(); } - Context context; - graph.SetAStarParents(true /* forward */, context.GetParents()); + Context context(graph); PeriodicPollCancellable periodicCancellable(params.m_cancellable); auto visitVertex = [&](Vertex const & vertex) { diff --git a/routing/base/astar_graph.hpp b/routing/base/astar_graph.hpp index cf38cfca82..14c7180328 100644 --- a/routing/base/astar_graph.hpp +++ b/routing/base/astar_graph.hpp @@ -22,6 +22,7 @@ public: virtual void GetIngoingEdgesList(Vertex const & v, std::vector & edges) = 0; virtual void SetAStarParents(bool forward, Parents & parents); + virtual void DropAStarParents(); virtual bool AreWavesConnectible(Parents & forwardParents, Vertex const & commonVertex, Parents & backwardParents); @@ -32,6 +33,9 @@ template void AStarGraph::SetAStarParents(bool /* forward */, std::map & /* parents */) {} +template +void AStarGraph::DropAStarParents() {} + template bool AStarGraph::AreWavesConnectible(AStarGraph::Parents & /* forwardParents */, Vertex const & /* commonVertex */, diff --git a/routing/index_graph_starter.hpp b/routing/index_graph_starter.hpp index 0ad20dd50f..0888fb9225 100644 --- a/routing/index_graph_starter.hpp +++ b/routing/index_graph_starter.hpp @@ -117,6 +117,11 @@ public: m_graph.SetAStarParents(forward, parents); } + void DropAStarParents() override + { + m_graph.DropAStarParents(); + } + bool AreWavesConnectible(std::map & forwardParents, Vertex const & commonVertex, std::map & backwardParents) override { diff --git a/routing/index_graph_starter_joints.hpp b/routing/index_graph_starter_joints.hpp index 7f012c4fbf..9b54b74ef6 100644 --- a/routing/index_graph_starter_joints.hpp +++ b/routing/index_graph_starter_joints.hpp @@ -60,6 +60,11 @@ public: m_graph.SetAStarParents(forward, parents); } + void DropAStarParents() override + { + m_graph.DropAStarParents(); + } + bool AreWavesConnectible(std::map & forwardParents, Vertex const & commonVertex, std::map & backwardParents) override { diff --git a/routing/single_vehicle_world_graph.cpp b/routing/single_vehicle_world_graph.cpp index f15faf6370..08ab333704 100644 --- a/routing/single_vehicle_world_graph.cpp +++ b/routing/single_vehicle_world_graph.cpp @@ -251,6 +251,15 @@ void SingleVehicleWorldGraph::SetAStarParents(bool forward, map::kEmpty; + m_parentsForJoints.forward = &AStarParents::kEmpty; + + m_parentsForSegments.backward = &AStarParents::kEmpty; + m_parentsForSegments.forward = &AStarParents::kEmpty; +} + template NumMwmId GetCommonMwmInChain(vector const & chain) { diff --git a/routing/single_vehicle_world_graph.hpp b/routing/single_vehicle_world_graph.hpp index 906e6018b5..ee67666c0d 100644 --- a/routing/single_vehicle_world_graph.hpp +++ b/routing/single_vehicle_world_graph.hpp @@ -81,6 +81,7 @@ public: void SetAStarParents(bool forward, ParentSegments & parents) override; void SetAStarParents(bool forward, ParentJoints & parents) override; + void DropAStarParents() override; bool AreWavesConnectible(ParentSegments & forwardParents, Segment const & commonVertex, ParentSegments & backwardParents, diff --git a/routing/world_graph.cpp b/routing/world_graph.cpp index d7e3e8668c..716acab3bf 100644 --- a/routing/world_graph.cpp +++ b/routing/world_graph.cpp @@ -56,6 +56,7 @@ std::vector WorldGraph::GetSpeedCamInfo(Segment const void WorldGraph::SetAStarParents(bool forward, std::map & parents) {} void WorldGraph::SetAStarParents(bool forward, std::map & parents) {} +void WorldGraph::DropAStarParents() {} bool WorldGraph::AreWavesConnectible(ParentSegments & forwardParents, Segment const & commonVertex, ParentSegments & backwardParents, diff --git a/routing/world_graph.hpp b/routing/world_graph.hpp index ff2844b5c5..9821ef8779 100644 --- a/routing/world_graph.hpp +++ b/routing/world_graph.hpp @@ -93,6 +93,7 @@ public: virtual void SetAStarParents(bool forward, ParentSegments & parents); virtual void SetAStarParents(bool forward, ParentJoints & parents); + virtual void DropAStarParents(); virtual bool AreWavesConnectible(ParentSegments & forwardParents, Segment const & commonVertex, ParentSegments & backwardParents,