CARLA
Loading...
Searching...
No Matches
test_in_memory_map.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7// The Traffic Manager's InMemoryMap must free its waypoint graph when it is
8// destroyed. Its SimpleWaypoints link to each other (next, previous, left and
9// right) through shared pointers, so the graph is freed only if the map breaks
10// those cycles: before, every Traffic Manager shut down and every map rebuild
11// leaked the whole graph.
12
13#include "test.h"
14
15#include <carla/Memory.h>
16#include <carla/client/Map.h>
19
20#include <memory>
21#include <string>
22#include <vector>
23
26
27namespace {
28
29// A straight 200 m road with two driving lanes in the same direction: its
30// waypoints have next/previous links and left/right lane change links.
31std::string TwoLaneRoad() {
32 return R"(<?xml version="1.0" encoding="UTF-8"?>
33<OpenDRIVE>
34 <header revMajor="1" revMinor="4" name="" version="1"
35 north="100" south="-100" east="300" west="-100"/>
36 <road name="R0" length="200" id="0" junction="-1">
37 <type s="0" type="town"><speed max="50" unit="km/h"/></type>
38 <planView>
39 <geometry s="0" x="0" y="0" hdg="0" length="200"><line/></geometry>
40 </planView>
41 <elevationProfile><elevation s="0" a="0" b="0" c="0" d="0"/></elevationProfile>
42 <lateralProfile/>
43 <lanes>
44 <laneSection s="0">
45 <center><lane id="0" type="none" level="false"/></center>
46 <right>
47 <lane id="-1" type="driving" level="false">
48 <width sOffset="0" a="3.5" b="0" c="0" d="0"/>
49 </lane>
50 <lane id="-2" type="driving" level="false">
51 <width sOffset="0" a="3.5" b="0" c="0" d="0"/>
52 </lane>
53 </right>
54 </laneSection>
55 </lanes>
56 </road>
57</OpenDRIVE>
58)";
59}
60
61} // namespace
62
63TEST(InMemoryMap, frees_its_waypoints_when_destroyed) {
64 const auto world_map = carla::MakeShared<carla::client::Map>("two_lanes", TwoLaneRoad());
65
66 std::vector<std::weak_ptr<SimpleWaypoint>> waypoints;
67 bool linked_along = false;
68 bool linked_across = false;
69 {
70 auto local_map = std::make_shared<InMemoryMap>(world_map);
71 local_map->SetUp();
72 for (const auto &waypoint : local_map->GetDenseTopology()) {
73 linked_along = linked_along || !waypoint->GetNextWaypoint().empty();
74 linked_across = linked_across ||
75 waypoint->GetLeftWaypoint() != nullptr ||
76 waypoint->GetRightWaypoint() != nullptr;
77 waypoints.emplace_back(waypoint);
78 }
79 }
80
81 // The graph had the links that make cycles...
82 ASSERT_FALSE(waypoints.empty());
83 ASSERT_TRUE(linked_along);
84 ASSERT_TRUE(linked_across);
85 // ... and none of its waypoints outlived the map.
86 size_t alive = 0u;
87 for (const auto &waypoint : waypoints) {
88 alive += waypoint.expired() ? 0u : 1u;
89 }
90 EXPECT_EQ(alive, 0u) << alive << " of " << waypoints.size()
91 << " waypoints outlived their InMemoryMap";
92}
This class builds a discretized local map-cache.
Definition InMemoryMap.h:63
This is a simple wrapper class on Carla's waypoint object.
TEST(InMemoryMap, frees_its_waypoints_when_destroyed)