Skip to content

Commit

Permalink
Merge branch 'RasterParameter' into 'master'
Browse files Browse the repository at this point in the history
[RasterParameter] Use unique_ptr to fix mem leaks

See merge request ogs/ogs!4822
  • Loading branch information
endJunction committed Nov 27, 2023
2 parents 678e531 + 3aeeefa commit db89e1a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
20 changes: 11 additions & 9 deletions GeoLib/IO/NetCDFRasterReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ std::vector<double> 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<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)
{
netCDF::NcFile dataset(filepath.string(), netCDF::NcFile::read);
if (dataset.isNull())
Expand All @@ -168,7 +169,8 @@ GeoLib::Raster readNetCDF(std::filesystem::path const& filepath,
std::vector<double> 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<GeoLib::Raster>(header, raster_data.begin(),
raster_data.end());
}
#endif

Expand All @@ -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<GeoLib::Raster>(
FileIO::AsciiRasterInterface::readRaster((path / filename).string()));
if (raster == nullptr)
{
OGS_FATAL("Could not read raster from file '{}'.",
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion GeoLib/Raster.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class Raster final
struct NamedRaster
{
std::string raster_name;
GeoLib::Raster raster;
std::unique_ptr<GeoLib::Raster> raster;
};

} // namespace GeoLib
2 changes: 1 addition & 1 deletion ParameterLib/RasterParameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct RasterParameter final : public Parameter<double>
{
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};
}

Expand Down

0 comments on commit db89e1a

Please sign in to comment.