microsoft / PowerToys

Windows system utilities to maximize productivity
MIT License
111.04k stars 6.54k forks source link

[Peek] Previewer icon bitmaps are never deleted, leaking memory #34527

Closed daverayment closed 3 weeks ago

daverayment commented 2 months ago

Microsoft PowerToys version

0.83.0

Installation method

PowerToys auto-update

Running as admin

No

Area(s) with issue?

Peek

Steps to reproduce

Use Peek to preview an audio file, a 'special folder' (e.g. one containing images where their thumbnails are shown on the folder itself), an unsupported file, or a drive. The icon subsequently displayed in the Peek UI is retrieved using the IconHelper in the Peek.FilePreviewer.Previewers.Helpers namespace.

In IconHelper.GetImageAsync(), the handle for the retrieved icon bitmap is set to IntPtr.Zero immediately after being retrieved, instead of being disposed of using NativeMethods.DeleteObject in the finally block. The handle's resource is never freed and remains in memory:

    ...
    imageSource = hr == HResult.Ok ? await BitmapHelper.GetBitmapFromHBitmapAsync(hbitmap, isSupportingTransparency, cancellationToken) : null;

    hbitmap = IntPtr.Zero;
}
finally
{
    if (hbitmap != IntPtr.Zero)
    {
        NativeMethods.DeleteObject(hbitmap);
    }

I have a fix for this and other thumbnail-related issues. As they are interrelated, I would like to tackle them in a single PR.

✔️ Expected Behavior

After navigating to an item where an icon graphic is shown in the previewer, the unmanaged bitmap resource is correctly freed.

❌ Actual Behavior

The unmanaged bitmap memory for the icon is not freed, taking up process memory.

Other Software

No response

similar-issues-ai[bot] commented 2 months ago

We've found some similar issues:

If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue.

Note: You can give me feedback by 👍 or 👎 this comment.

jaimecbernardo commented 3 weeks ago

This issue has been worked on during the 0.85 development cycle. Please update to the latest release.