Cannot Close/Release Raster file (TIFF) after removal from layer

425
4
10-28-2021 12:40 AM
MarcusJones
New Contributor

Hi,

   I have an app that generates TIFF file on the fly based on sensor data. I am using a raster layer and colormap renderer to visualize the data. 

 

// create a raster from a path to a supported raster format
Raster myRaster = new Raster(vm.RadarLoc.rasterPath);
               
// create a RasterLayer using the Raster
newRasterLayer = new RasterLayer(myRaster);
newRasterLayer.Opacity=0.7;
ApplyColour();

//  newRasterLayer.Renderer = 
MyMapView.Map.OperationalLayers.Add(newRasterLayer);

 

  When a new set of data is available I remove the Raster from the layers - using MyMapView.Map.OperationalLayers.Remove(newRasterLayer);. I then deference the newRasterLayer. I would hope that the backing file would now have any file handle closed within the ERSI runtime, however if I try to delete it or modify it I cannot because the file is still open.

 

// Remove the raster
MyMapView.Map.OperationalLayers.Remove(newRasterLayer);
                    
newRasterLayer= null;

 

  I have not been able to find a way to get the file closed. GCCollect does not help, waiting for a time does not help. Closing the app totally does release it 🙂

Added a small demo of the problem.

  Am I missing a step to get the file released?

c#, SDK WPF Runtime 100.10.0.

0 Kudos
4 Replies
PreetiMaske
Esri Contributor

Hi,
Try setting the local raster i.e "myRaster" raster object in code above , to null as well. It might be raster object that is perhaps locked.

Thanks

0 Kudos
MarcusJones
New Contributor

Hi,

 The myRaster variable only has local scope in the event handler and so goes out of scope immediately after the Raster layer has been added. However I have also tried setting it to null and get the same issue with the file remaining open.

 

0 Kudos
MichaelBranscomb
Esri Frequent Contributor

Hi,

It's possible that you're unable to delete the raster until .NET garbage collection has run. Try calling the following (one or more times):

 

GC.Collect();
GC.WaitForPendingFinalizers();
try
{
    if (File.Exists(rasterFile))
    {
        File.Delete(rasterFile);
    }
}
catch (Exception e)
{
    // TODO...
}

 

 

0 Kudos
MarcusJones
New Contributor

Hi,

  Thanks for the suggestion. I have tried that - and added a loop in to retry and wait for 1 sec at a time after triggering the garbage collector. However the problem remains. 

0 Kudos