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

730
7
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
7 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
RichardFlapper
New Contributor II

Did you even managed to solve this Marcus? I'm running in exactly the same situation with the 200.0 SDK. I've tried all kinds of solutions. When running from Visual Studio 2022 all runs well but when you build and install the program it runs into this situation. For some reason the Raster is keeping the file locked even though it supposedly is released (after setting the Raster to null and having removed all layers on the map).

0 Kudos
MarcusJones
New Contributor

Hi,

   No, sorry. I couldn't find a way to get the file closed, so I avoided using the same file name and did a tidy up when the application was closed.

0 Kudos
RichardFlapper
New Contributor II

Ah, too bad. Guess I'll have to use the same tactic then and use completely random file names. Thanks for replying!  

0 Kudos