From aaf9470ec986363190e447308edee0f4ce65dd04 Mon Sep 17 00:00:00 2001 From: Thomas Fischer Date: Tue, 23 May 2023 16:48:09 +0200 Subject: [PATCH 1/3] [GL/Raster] Use unique_ptr to avoid mem leak --- GeoLib/Raster.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GeoLib/Raster.h b/GeoLib/Raster.h index c2b24c7b060..6234c0395ff 100644 --- a/GeoLib/Raster.h +++ b/GeoLib/Raster.h @@ -154,7 +154,7 @@ class Raster final struct NamedRaster { std::string raster_name; - GeoLib::Raster raster; + std::unique_ptr raster; }; } // namespace GeoLib From cd65a8e5ad27616ebac84a4a2f6c6a3cd3236456 Mon Sep 17 00:00:00 2001 From: Thomas Fischer Date: Tue, 23 May 2023 16:49:03 +0200 Subject: [PATCH 2/3] [ParameterLib] Access value via unique_ptr --- ParameterLib/RasterParameter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ParameterLib/RasterParameter.h b/ParameterLib/RasterParameter.h index e864560b095..436fddfb41e 100644 --- a/ParameterLib/RasterParameter.h +++ b/ParameterLib/RasterParameter.h @@ -35,7 +35,7 @@ struct RasterParameter final : public Parameter { OGS_FATAL("RasterParameter::operator(): couldn't get coordinates."); } - auto const value = _named_raster.raster.getValueAtPoint(*coordinates); + auto const value = _named_raster.raster->getValueAtPoint(*coordinates); return {value}; } From 3aeeefa7458f1466aa34980fdf957671255951f1 Mon Sep 17 00:00:00 2001 From: Thomas Fischer Date: Thu, 23 Nov 2023 09:56:59 +0100 Subject: [PATCH 3/3] [GL/IO/NetCDF] Use unique_ptr to prevent mem leaks --- GeoLib/IO/NetCDFRasterReader.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/GeoLib/IO/NetCDFRasterReader.cpp b/GeoLib/IO/NetCDFRasterReader.cpp index b5d4800ce67..ab5f28b27d1 100644 --- a/GeoLib/IO/NetCDFRasterReader.cpp +++ b/GeoLib/IO/NetCDFRasterReader.cpp @@ -152,10 +152,11 @@ std::vector readDataFromNetCDF( return raster_data; } -GeoLib::Raster readNetCDF(std::filesystem::path const& filepath, - std::string_view const var_name, - std::size_t const dimension_number, - GeoLib::MinMaxPoints const& min_max_points) +std::unique_ptr readNetCDF( + std::filesystem::path const& filepath, + std::string_view const var_name, + std::size_t const dimension_number, + GeoLib::MinMaxPoints const& min_max_points) { netCDF::NcFile dataset(filepath.string(), netCDF::NcFile::read); if (dataset.isNull()) @@ -168,7 +169,8 @@ GeoLib::Raster readNetCDF(std::filesystem::path const& filepath, std::vector raster_data = readDataFromNetCDF( variables, var_name, dimension_number, min_max_points, header); - return GeoLib::Raster{header, raster_data.begin(), raster_data.end()}; + return std::make_unique(header, raster_data.begin(), + raster_data.end()); } #endif @@ -190,14 +192,14 @@ GeoLib::NamedRaster readRasterFromFile( return GeoLib::NamedRaster{filename.replace_extension().string() + "_" + var_name + "_" + std::to_string(dimension_number), - raster}; + std::move(raster)}; #else OGS_FATAL("OGS was not build with NetCDF support. Can not read {}", (path / filename).string()); #endif } - auto* raster = - FileIO::AsciiRasterInterface::readRaster((path / filename).string()); + auto raster = std::unique_ptr( + FileIO::AsciiRasterInterface::readRaster((path / filename).string())); if (raster == nullptr) { OGS_FATAL("Could not read raster from file '{}'.", @@ -206,7 +208,7 @@ GeoLib::NamedRaster readRasterFromFile( return GeoLib::NamedRaster{filename.replace_extension().string() + "_" + var_name + "_" + std::to_string(dimension_number), - *raster}; + std::move(raster)}; } } // anonymous namespace