BitmapDecoder not releasing file lock

R

Roger Martin

I am having an issue related to my original thread here:

http://www.microsoft.com/communitie...1d640f3bd725&lang=en&cr=US&sloc=en-us&m=1&p=1

In that thread you helped me figure out how to ensure that the BitmapDecoder
releases the file lock. I have discovered that, while it CORRECTLY releases
the file lock when a valid image is passed to the BitmapDecoder.Create()
method, it DOES NOT release the lock when the method throws an exception.

Specifically, certain images, such as WMF files, throw an exception when
passed to the Create() method. When this occurs, the file lock is not
released. When this happens in my web application, I am unable to delete the
file unless I cycle the app pool or call GC.Collect.

Here is code to reproduce:

using System;
using System.Windows.Media.Imaging;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string imageFilePath = @"D:\BD00146_.WMF";
BitmapMetadata bmp = GetBitmapMetadata(imageFilePath);
}

private static BitmapMetadata GetBitmapMetadata(string imageFilePath)
{
BitmapDecoder fileBitmapDecoder;
try
{
fileBitmapDecoder = BitmapDecoder.Create(new Uri(imageFilePath,
UriKind.Absolute), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
catch (NotSupportedException)
{
// Lock is not released unless I perform a garbage collection.
//GC.Collect();
return null;
}

return null;
}
}
}

Note: The file lock will occur on any file that generates an exception. If
you can't find a WMF file for testing, you can use a plain text file.

It is impossible to accurately predict which files will end up throwing an
exception, so I need the Create method to release the lock on its own or I
need a way to release the file lock within the catch block. Since the
exception will rarely occur, the only solution may be to run the garbage
collector in the catch block, but I don't think this is the best practice
recommended by MS.

If you determine this is a bug, can you create the appropriate paperwork so
that it might be fixed or tell me where I should submit it?

Cheers,
Roger Martin
Gallery Server Pro - Open Source Web App for Photos, Video, Audio and
Documents
www.galleryserverpro.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top