Fix GDAL dataset leak in RasterLayer::loadAndReprojectFile (#96)#114
Merged
Conversation
added 11 commits
June 22, 2026 01:57
RAII unique_ptr fix for both GDAL handles in loadAndReprojectFile + regression test via GDALDataset::GetOpenDatasets
loadAndReprojectFile opened a source dataset (GDALOpen) and a warped VRT
(GDALAutoCreateWarpedVRT) and closed neither, leaking both on every return
path — normal return, the !reprojected_dataset early return (leaked the
source), and the abort-flag return {} paths (leaked both). Triggered on every
RasterLayer ctor, setColormap(), and readSettings().
Wrap both handles in std::unique_ptr with a GDALClose deleter so all paths
free them automatically. dataset stays declared before reprojected_dataset so
reverse-declaration destruction closes the warped VRT first, then the source
(the VRT references the source) — matching initExtent(); a comment records the
ordering constraint.
Add test_raster_layer_gdal_cleanup: write a synthetic WGS84 single-band Float32 GeoTIFF, construct a RasterLayer, flip the colormap twice (each re-invokes loadAndReprojectFile), destroy the layer, and assert the process-wide open-dataset count returns to its pre-construction baseline. Baseline-delta rather than == 0, since GDALDataset::GetOpenDatasets() counts all process-wide handles. Guards against a vacuous pass by asserting each load actually reached the warp path and succeeded (status() == "") — a load that never warps never opens the handles under test. Verified the test fails when the RAII close is removed. Also sync plan.md with the as-built fix (three folded Plan Review resolutions; open questions resolved).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #96
Summary
RasterLayer::loadAndReprojectFileopened two GDAL datasets (GDALOpensource +GDALAutoCreateWarpedVRTwarped VRT) and never closed either — leaking both on every return path (normal, the!reprojected_datasetearly return that leaked the source, and the abort-flagreturn {}paths). Triggered by theRasterLayerctor,setColormap(), andreadSettings(), so flipping a colormap ramp leaked two handles per flip.Fix
std::unique_ptr<GDALDataset, …>with aGDALClosedeleter, so all return/abort paths free both automatically (mirrors the siblinginitExtent()).datasetdeclared beforereprojected_dataset, so reverse-declaration destruction closes the warped VRT first, source second (the VRT references the source); a comment records the constraint.Test
New
test/test_raster_layer_gdal_cleanup.cpp: writes a synthetic 8×8 WGS84 Float32 GeoTIFF, constructs aRasterLayer, flips the colormap twice (each re-invokesloadAndReprojectFile), destroys the layer, and asserts the process-wide open-dataset count returns to its pre-construction baseline (delta == 0). The test asserts each load actually reached the warp path (viastatus()), so it can't pass vacuously — verified by a neutered-deleter check producing delta == 6.102 tests, 0 failures (2 pre-existing GL-gated skips).
Scope note
This is a real, bounded leak (load / colormap-change), not the cause of the #98 zoom/pan OOM —
RasterLayerhas no viewport handler and reprojects once, so zoom/pan doesn't re-invoke this path. #98's OOM isMapTilestile accumulation, tracked separately.Authored-By:
Claude Code AgentModel:
Claude Opus 4.8