I have the following C# code to mosaic multiple rasters using GDAL C# bindings and GdalWarp method:
try
{
Gdal.AllRegister();
// Open all the raster files in read-only mode
var datasets = new Dataset[_filesToMerge.Count];
for (int i = 0; i < _filesToMerge.Count; i++)
datasets[i] = Gdal.Open(_filesToMerge[i], Access.GA_ReadOnly);
// Set the warp options for the merge
var warpOptions = new string[] {};
var progressDelegate = new Gdal.GDALProgressFuncDelegate(Progress);
// Perform the merge using GDAL
// Gdal.Warp(_outputFileName, datasets, new GDALWarpAppOptions(warpOptions), new Gdal.GDALProgressFuncDelegate(Progress), "");
var options = new GDALWarpAppOptions(warpOptions);
var result = Gdal.Warp(_outputFileName, datasets, options, null, "warp");
for (int i = 0; i < datasets.Length; i++)
datasets[i].Close();
warpOptions = null;
return true;
}
catch (Exception ex) { }
{
return false;
}
The code works well and creates the raster as expected. The problem starts right after a few seconds (sometimes 10), and while the app running, the following exception is thrown.
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
The problem occurs whenever i attempt to close the datasets after gdalwarp method is completed. If I dont close the datasets, no exception occurs but in that case, the files remain open till the app closes down.
I am sure that there is something remaining alive during the gdalwarp method call and when i try to close the datasets, it breaks remaining stuff related to the gdalwrap.
I am using GDAL 3.8.5 from nuget. App is windows forms using .net 4.8 and build in x64.
Is there any suggestion to propoerly close datasets after gdalwarp call while using C# bindings of GDAL ?