Skip to content

Commit

Permalink
Fixed COM instance leak and error (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
shibayan authored Jan 9, 2021
1 parent 2384365 commit 6536679
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 172 deletions.
48 changes: 28 additions & 20 deletions WinQuickLook/Controls/PreviewHandlerHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,38 +88,46 @@ public IPreviewHandler CreatePreviewHandler(string fileName)
return null;
}

var initializeWithFile = previewHandler.QueryInterface<IInitializeWithFile>();

if (initializeWithFile != null)
try
{
initializeWithFile.Initialize(fileName, 0);
var initializeWithFile = previewHandler.QueryInterface<IInitializeWithFile>();

return previewHandler;
}
if (initializeWithFile != null)
{
initializeWithFile.Initialize(fileName, 0);

var initializeWithItem = previewHandler.QueryInterface<IInitializeWithItem>();
return previewHandler;
}

if (initializeWithItem != null)
{
NativeMethods.SHCreateItemFromParsingName(fileName, IntPtr.Zero, typeof(IShellItem).GUID, out var shellItem);
var initializeWithItem = previewHandler.QueryInterface<IInitializeWithItem>();

initializeWithItem.Initialize(shellItem, 0);
if (initializeWithItem != null)
{
NativeMethods.SHCreateItemFromParsingName(fileName, IntPtr.Zero, typeof(IShellItem).GUID, out var shellItem);

return previewHandler;
}
initializeWithItem.Initialize(shellItem, 0);

try
{
previewHandler.Unload();
return previewHandler;
}

var initializeWithStream = previewHandler.QueryInterface<IInitializeWithStream>();

if (initializeWithStream != null)
{
initializeWithStream.Initialize(new ComStream(File.Open(fileName, FileMode.Open, FileAccess.Read)), 0);

return previewHandler;
}
}
catch (Exception ex)
{
Crashes.TrackError(ex);
}
finally
{
Marshal.FinalReleaseComObject(previewHandler);
}

// 初期化できない場合はアンロードする
previewHandler.Unload();

Marshal.FinalReleaseComObject(previewHandler);

return null;
}
Expand Down
7 changes: 3 additions & 4 deletions WinQuickLook/Extensions/QuickLookHandlerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Windows;

using Microsoft.AppCenter.Crashes;
Expand All @@ -12,18 +11,18 @@ internal static class QuickLookHandlerExtensions
{
private static readonly IQuickLookHandler _genericHandler = new GenericQuickLookHandler();

public static async Task<(FrameworkElement, Size, string)> GetViewerWithErrorAsync(this IQuickLookHandler handler, string fileName)
public static (FrameworkElement, Size, string) GetViewerWithHandleError(this IQuickLookHandler handler, string fileName)
{
try
{
return await (handler ?? _genericHandler).GetViewerAsync(fileName);
return (handler ?? _genericHandler).GetViewer(fileName);
}
catch (Exception ex)
{
Crashes.TrackError(ex);
}

return await _genericHandler.GetViewerAsync(fileName);
return _genericHandler.GetViewer(fileName);
}
}
}
7 changes: 3 additions & 4 deletions WinQuickLook/Handlers/AnimatedGifQuickLookHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
Expand All @@ -25,7 +24,7 @@ public bool CanOpen(string fileName)
return bitmap.Frames.Count > 1;
}

public async Task<(FrameworkElement, Size, string)> GetViewerAsync(string fileName)
public (FrameworkElement, Size, string) GetViewer(string fileName)
{
using var file = TagLib.File.Create(fileName);

Expand All @@ -41,8 +40,8 @@ public bool CanOpen(string fileName)
mediaElement.Source = new Uri(fileName, UriKind.Absolute);
mediaElement.LoadedBehavior = MediaState.Play;
mediaElement.UnloadedBehavior = MediaState.Manual;
mediaElement.MediaOpened += (_, __) => mediaElement.Play();
mediaElement.MediaEnded += (_, __) =>
mediaElement.MediaOpened += (_, _) => mediaElement.Play();
mediaElement.MediaEnded += (_, _) =>
{
mediaElement.Position = TimeSpan.FromMilliseconds(1);
mediaElement.Play();
Expand Down
3 changes: 1 addition & 2 deletions WinQuickLook/Handlers/AudioQuickLookHandler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Windows;

using WinQuickLook.Controls;
Expand All @@ -18,7 +17,7 @@ public bool CanOpen(string fileName)
return _supportFormats.Contains(extension);
}

public async Task<(FrameworkElement, Size, string)> GetViewerAsync(string fileName)
public (FrameworkElement, Size, string) GetViewer(string fileName)
{
var requestSize = new Size(600, 300);

Expand Down
4 changes: 1 addition & 3 deletions WinQuickLook/Handlers/ComInteropQuickLookHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms.Integration;

Expand All @@ -16,7 +14,7 @@ public bool CanOpen(string fileName)
return PreviewHandlerHost.GetPreviewHandlerCLSID(fileName) != Guid.Empty;
}

public async Task<(FrameworkElement, Size, string)> GetViewerAsync(string fileName)
public (FrameworkElement, Size, string) GetViewer(string fileName)
{
var requestSize = new Size
{
Expand Down
3 changes: 1 addition & 2 deletions WinQuickLook/Handlers/GenericQuickLookHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Threading.Tasks;
using System.Windows;

using WinQuickLook.Controls;
Expand All @@ -14,7 +13,7 @@ public bool CanOpen(string fileName)
return true;
}

public async Task<(FrameworkElement, Size, string)> GetViewerAsync(string fileName)
public (FrameworkElement, Size, string) GetViewer(string fileName)
{
var requestSize = new Size(572, 290);

Expand Down
3 changes: 1 addition & 2 deletions WinQuickLook/Handlers/HtmlQuickLookHandler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

Expand All @@ -21,7 +20,7 @@ public bool CanOpen(string fileName)
return _supportFormats.Contains(extension);
}

public async Task<(FrameworkElement, Size, string)> GetViewerAsync(string fileName)
public (FrameworkElement, Size, string) GetViewer(string fileName)
{
var requestSize = new Size
{
Expand Down
5 changes: 2 additions & 3 deletions WinQuickLook/Handlers/IQuickLookHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows;

namespace WinQuickLook.Handlers
{
public interface IQuickLookHandler
{
bool CanOpen(string fileName);

Task<(FrameworkElement, Size, string)> GetViewerAsync(string fileName);
(FrameworkElement, Size, string) GetViewer(string fileName);
}
}
3 changes: 1 addition & 2 deletions WinQuickLook/Handlers/ImageQuickLookHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
Expand Down Expand Up @@ -28,7 +27,7 @@ public bool CanOpen(string fileName)
}
}

public async Task<(FrameworkElement, Size, string)> GetViewerAsync(string fileName)
public (FrameworkElement, Size, string) GetViewer(string fileName)
{
var (bitmap, originalSize) = GetImage(fileName);

Expand Down
31 changes: 24 additions & 7 deletions WinQuickLook/Handlers/InternetShortcutQuickLookHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;

namespace WinQuickLook.Handlers
Expand All @@ -17,9 +19,9 @@ public bool CanOpen(string fileName)
return _supportFormats.Contains(extension);
}

public async Task<(FrameworkElement, Size, string)> GetViewerAsync(string fileName)
public (FrameworkElement, Size, string) GetViewer(string fileName)
{
var content = await File.ReadAllLinesAsync(fileName);
var content = File.ReadAllLines(fileName);

var urlEntry = content.FirstOrDefault(x => x.StartsWith("URL"));

Expand All @@ -36,11 +38,26 @@ public bool CanOpen(string fileName)
Height = 900
};

var webView2 = new WebView2();
try
{
CoreWebView2Environment.GetAvailableBrowserVersionString();

webView2.Loaded += (sender, e) => ((WebView2)sender).CoreWebView2.Navigate(url);
var webView2 = new WebView2
{
Source = new Uri(url)
};

return (webView2, requestSize, url);
return (webView2, requestSize, url);
}
catch (EdgeNotFoundException)
{
var webBrowser = new WebBrowser
{
Source = new Uri(url)
};

return (webBrowser, requestSize, url);
}
}

private static readonly IList<string> _supportFormats = new[]
Expand Down
5 changes: 2 additions & 3 deletions WinQuickLook/Handlers/PdfQuickLookHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms.Integration;

Expand All @@ -19,11 +18,11 @@ public bool CanOpen(string fileName)
return extension == ".pdf";
}

public async Task<(FrameworkElement, Size, string)> GetViewerAsync(string fileName)
public (FrameworkElement, Size, string) GetViewer(string fileName)
{
var pdfViewer = new PdfViewer();

var document = PdfDocument.Load(new MemoryStream(await File.ReadAllBytesAsync(fileName)));
var document = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(fileName)));

pdfViewer.Document = document;

Expand Down
3 changes: 1 addition & 2 deletions WinQuickLook/Handlers/SvgQuickLookHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;
Expand All @@ -23,7 +22,7 @@ public bool CanOpen(string fileName)
return _supportFormats.Contains(extension);
}

public async Task<(FrameworkElement, Size, string)> GetViewerAsync(string fileName)
public (FrameworkElement, Size, string) GetViewer(string fileName)
{
var requestSize = GetScaledImageSize(fileName, 1200);

Expand Down
3 changes: 1 addition & 2 deletions WinQuickLook/Handlers/SyntaxHighlightQuickLookHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

Expand All @@ -22,7 +21,7 @@ public bool CanOpen(string fileName)
return HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(fileName)) != null;
}

public async Task<(FrameworkElement, Size, string)> GetViewerAsync(string fileName)
public (FrameworkElement, Size, string) GetViewer(string fileName)
{
var requestSize = new Size
{
Expand Down
Loading

0 comments on commit 6536679

Please sign in to comment.