Skip to content

Commit

Permalink
Fixed: memory leak with Country_aos arrangement (using std::shared_pt…
Browse files Browse the repository at this point in the history
…r with custom deleter)
  • Loading branch information
denizdiktas committed Aug 15, 2023
1 parent 60a9ce2 commit db65cfa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
26 changes: 20 additions & 6 deletions Arrangement_on_surface_2/demo/earth/Aos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1537,30 +1537,44 @@ namespace
}
}

namespace {

Aos::Arr_handle get_handle(void* arr)
{
return Aos::Arr_handle(arr, [](void* ptr)
{
std::cout << "*** DELETING THE ARRANGEMENT WITH SHARED_PTR DELETER!!\n";
delete reinterpret_cast<Countries_arr*>(ptr);
});
}
}

Aos::Arr_handle Aos::load_arr(const std::string& file_name)
{
auto* arr = new Countries_arr(&s_traits);
auto arrh = get_handle(arr);

Kernel kernel;
auto rc = read_arrangement(file_name, *arr, kernel);
if (!rc) {
std::cerr << "Failed to load database!\n";
return nullptr;
}

return arr;
return arrh;
}


Aos::Arr_handle Aos::construct(Kml::Placemarks& placemarks)
{
auto* arr = new Arrangement(&s_traits);
auto arrh = get_handle(arr);

auto xcvs = get_arcs(placemarks, *arr);
for (auto& xcv : xcvs)
CGAL::insert(*arr, xcv);

return arr;
return arrh;
}


Expand All @@ -1577,7 +1591,7 @@ std::vector<QVector3D> Aos::get_triangles(Arr_handle arrh)
using Point = CDT::Point;
using Polygon_2 = CGAL::Polygon_2<K>;

auto& arr = *reinterpret_cast<Arrangement*>(arrh);
auto& arr = *reinterpret_cast<Arrangement*>(arrh.get());

//Geom_traits traits;
auto approx = s_traits.approximate_2_object();
Expand Down Expand Up @@ -1701,7 +1715,7 @@ Aos::Country_triangles_map Aos::get_triangles_by_country(Arr_handle arrh)
using Point = CDT::Point;
using Polygon_2 = CGAL::Polygon_2<K>;

auto& arr = *reinterpret_cast<Countries_arr*>(arrh);
auto& arr = *reinterpret_cast<Countries_arr*>(arrh.get());
auto approx = s_traits.approximate_2_object();

// group the faces by their country name
Expand Down Expand Up @@ -1825,7 +1839,7 @@ Aos::Country_triangles_map Aos::get_triangles_by_country(Arr_handle arrh)

Aos::Country_color_map Aos::get_color_mapping(Arr_handle arrh)
{
auto& arr = *reinterpret_cast<Countries_arr*>(arrh);
auto& arr = *reinterpret_cast<Countries_arr*>(arrh.get());

// group the faces by their country name,
std::vector<std::string> all_countries;
Expand Down Expand Up @@ -1907,7 +1921,7 @@ std::string Aos::locate_country(Arr_handle arrh, const QVector3D& point)
using Point_2 = Countries_arr::Point_2;
using Naive_pl = CGAL::Arr_naive_point_location<Countries_arr>;

auto& arr = *reinterpret_cast<Countries_arr*>(arrh);
auto& arr = *reinterpret_cast<Countries_arr*>(arrh.get());
Point_2 query_point(Dir3(point.x(), point.y(), point.z()),
Point_2::Location_type::NO_BOUNDARY_LOC);

Expand Down
4 changes: 3 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Aos.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define AOS_H

#include <map>
#include <memory>
#include <vector>
#include <qvector3d.h>

Expand All @@ -22,7 +23,8 @@ class Aos
public:
using Approx_arc = std::vector<QVector3D>;
using Approx_arcs = std::vector<Approx_arc>;
using Arr_handle = void*;
//using Arr_handle = void*;
using Arr_handle = std::shared_ptr<void>;

static Approx_arc get_approx_identification_curve(double error);

Expand Down

0 comments on commit db65cfa

Please sign in to comment.