是什么原因导致ExtractIconExW泄漏内存?

问题描述 投票:0回答:1

当我调用

ExtractIconExW
数百次时,我的应用程序因各种随机错误而崩溃,主要是与“内存不足”相关(即使任务管理器指示它只使用了几百兆字节)。

我尝试使用

DeleteObject
功能进行清理,但无济于事。

这是我的实现:

Public Shared Function ExtractIcon(ref As String) As BitmapSource
    Dim s() As String = Split(ref, ","), icon As IntPtr, iconl As IntPtr
    Try
        Functions.ExtractIconEx(s(0), s(1), iconl, icon, 1)
        If Not IntPtr.Zero.Equals(icon) Then
            Return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()))
        End If
    Finally
        Functions.DeleteObject(icon)
        Functions.DeleteObject(iconl)
    End Try
    Return Nothing
End Function

我使用这些声明:

<DllImport("Shell32.dll", EntryPoint:="ExtractIconExW", CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function ExtractIconEx(sFile As String, iIndex As Integer, ByRef piLargeVersion As IntPtr, ByRef piSmallVersion As IntPtr, amountIcons As Integer) As Integer
End Function

<DllImport("gdi32.dll", SetLastError:=True)>
Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
End Function
.net windows vb.net winapi
1个回答
0
投票

ExtractIconEx
的文档指出:

当不再需要它们时,您必须销毁所有提取的图标 通过 ExtractIconEx 通过调用 DestroyIcon 函数来实现。

这意味着致电

DeleteObject
不是您应该做的。为了正确清理,您应该致电
DestroyIcon

vb.net 的 Pinvoke 签名 是:

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function DestroyIcon(ByVal hIcon As IntPtr) As Boolean
End Function
© www.soinside.com 2019 - 2024. All rights reserved.