- darthmaule2
- Posts: 98
- Joined:
BitmapImage resources created from images in the filesystem cannot be deleted
So we have this custom FileTextureProvide which can create resources from the filesystem
That code is triggered when we access those resources via BitmapImage, like:
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?
Code: Select all
public class FileSystemTextureProvider : FileTextureProvider
{
...
public override Stream OpenStream(string path)
{
...
return new FileStream(path, FileMode.Open, FileAccess.Read);
}
Code: Select all
BitmapImage bitmapImage = new BitmapImage(new Uri(file.FullName));
Is there a way to tell Noesis to let go of the file?
- darthmaule2
- Posts: 98
- Joined:
Re: BitmapImage resources created from images in the filesystem cannot be deleted
And 2 minutes later I realized I could add "FileShare.Delete" to the FileStream call:
Code: Select all
new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Delete);
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: BitmapImage resources created from images in the filesystem cannot be deleted
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?
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
- Posts: 98
- Joined:
Re: BitmapImage resources created from images in the filesystem cannot be deleted
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);
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);
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: BitmapImage resources created from images in the filesystem cannot be deleted
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.
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.
- darthmaule2
- Posts: 98
- Joined:
Re: BitmapImage resources created from images in the filesystem cannot be deleted
I opened a ticket for a way the framework could tell me when it's safe/OK to dispose the FileStream object.
https://noesisengine.com/bugs/view.php?id=1554
https://noesisengine.com/bugs/view.php?id=1554
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: BitmapImage resources created from images in the filesystem cannot be deleted
Thanks for the report.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 33 guests