darthmaule2
Topic Author
Posts: 83
Joined: 23 Oct 2014, 19:54

BitmapImage resources created from images in the filesystem cannot be deleted

11 Sep 2019, 22:03

So we have this custom FileTextureProvide which can create resources from the filesystem
    public class FileSystemTextureProvider : FileTextureProvider
    {
	...
        public override Stream OpenStream(string path)
        {
	    ...
            return new FileStream(path, FileMode.Open, FileAccess.Read);
        }
That code is triggered when we access those resources via BitmapImage, like:
    BitmapImage bitmapImage = new BitmapImage(new Uri(file.FullName));
The issue is that, when I later try to delete the underlying file, I get a System.IO.IOException "The process cannot access the file because it is being used by another process"... which I assume is because the Noesis framework has this file open.

Is there a way to tell Noesis to let go of the file?

Tags:
 
darthmaule2
Topic Author
Posts: 83
Joined: 23 Oct 2014, 19:54

Re: BitmapImage resources created from images in the filesystem cannot be deleted

11 Sep 2019, 22:08

And 2 minutes later I realized I could add "FileShare.Delete" to the FileStream call:
new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Delete);
 
User avatar
sfernandez
Site Admin
Posts: 1624
Joined: 22 Dec 2011, 19:20

Re: BitmapImage resources created from images in the filesystem cannot be deleted

12 Sep 2019, 09:35

For textures stream is only used to load the texture data, once that is done we release it.
So in C#, when the garbage collector kicks in it should destroy the stream and free any lock that could be opened.
Is that not happening in your application?
 
darthmaule2
Topic Author
Posts: 83
Joined: 23 Oct 2014, 19:54

Re: BitmapImage resources created from images in the filesystem cannot be deleted

13 Sep 2019, 12:08

Actually no... I'm getting periodic IOExceptions when I try to delete files which I'm displaying as thumbnails in my file manager view.

I think you have to explicitly Dispose the stream.
Normally I would wrap this line in a using block to ensure it's Disposed when out of scope, but if I do that, it breaks my thumbnails (but fixes the IOExceptions):
return new FileStream(path, FileMode.Open, FileAccess.Read);
 
User avatar
sfernandez
Site Admin
Posts: 1624
Joined: 22 Dec 2011, 19:20

Re: BitmapImage resources created from images in the filesystem cannot be deleted

Yesterday, 13:08

We shouldn't Dispose/Close the stream because we are not the owners of that object, just consumers, we don't know if user wants to keep that stream open for other purposes. But it is not clear from the resource provider API when that stream is being used and when it can be safely released. We will think about this to see if we can improve it.

Anyway, I just created a small WPF test that opens a file with a FileStream, reads it and prints the contents to the console. While I keep a strong reference to the stream I cannot delete the file from disk, but as soon as I release that reference and call GC.Collect() I can delete it from disk. Maybe the problem was that we still had a strong reference to the stream when you tried to delete the file or that GC wasn't really being yet executed. We'll investigate what happens in Noesis and let you know.

Who is online

Users browsing this forum: No registered users and 2 guests