1

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 ?

3
  • In the Python datasets I can see that dataset is set to None and gdal.Unlink is used github.com/rouault/gdal/blob/rfc_86/autotest/utilities/…. Maybe that can give you some hint for C#.
    – user30184
    Commented May 19 at 10:38
  • The only option that i succeed is to set datasets to null, call Gdal.Unlink and wait GC to collect null variables. this way no Access Violation occurs and files remain closed. Commented May 19 at 17:44
  • There's a related post that uses a different overload, in that post the warping is done in a function, the datasets are not implicitly closed but handled by the GC when the function ends and the dataset(s) go out of scope, it might be worth a try, read more gis.stackexchange.com/questions/297831/… the pinning of the GC in that thread makes me think that the warp is happening on a different thread and you're closing the datasets while warp is still using them, i.e. a race conditon. Commented May 20 at 0:30

0

Browse other questions tagged or ask your own question.