FileOnQ / Imaging.Heif

A C#/.NET wrapper around libheif for decoding and processing high efficiency image formats (heif, heic).
GNU Lesser General Public License v3.0
15 stars 4 forks source link

File handle not closed/disposed if exception is thrown #99

Open swingnchad opened 1 year ago

swingnchad commented 1 year ago

If I pass an invalid or corrupt heic file to the HeifImage constructor, an exception is thrown without closing the file. If I try to delete the file I get an access violation exception.

            try
            {                
                using (var img = new HeifImage(heicImageFile)) // HeifException thrown here: "Heif image exception - Invalid input: No 'ftyp' box"
                using (var primary = img.PrimaryImage())
                {
                    primary.Write(jpegImageFile, 100);
                }

                File.Delete(heicImageFile);                
            }
            catch (Exception ex)
            {
                File.Delete(heicImageFile); // IOException thrown here: "The process cannot access the file xxx.heic because it is being used by another process."
            }

I would suggest you close/dispose the file handle in the finally block of a try/catch/finally so there are no memory leaks or file handles left open from an exception.