gpt4 book ai didi

c# - Windows 10 上的 .NET FontFamily 内存泄漏

转载 作者:行者123 更新时间:2023-11-30 12:57:37 24 4
gpt4 key购买 nike

在 Windows 10 上,System.Drawing.FontFamily.IsStyleAvailable 方法似乎将分配的空间留在内存中,即使在调用 Dispose 方法之后也是如此。

我写了一个简单的控制台应用程序来测试它:

using System;
using System.Drawing;
using System.Diagnostics;

namespace ConsoleApplication1
{
class Program
{
static string getMemoryStatusString()
{
using (Process p = Process.GetCurrentProcess())
{
return "(p: " + p.PrivateMemorySize64 + ", v:" + p.VirtualMemorySize64 + ")";
}
}

static void Main(string[] args)
{
string s = getMemoryStatusString();
foreach(FontFamily fontFamily in FontFamily.Families)
{
Console.Write(fontFamily.Name + " " + getMemoryStatusString() + " -> ");

fontFamily.IsStyleAvailable(FontStyle.Regular);
fontFamily.Dispose();

Console.WriteLine(getMemoryStatusString());
}
string e = getMemoryStatusString();
Console.WriteLine(s + " -> " + e);
Console.ReadLine();
}
}
}

知道为什么会这样吗?

提前致谢!

最佳答案

如果存在内存泄漏,它将在 gdiplus.dll 中, FontFamily.IsStyleAvailable()实际上对 GdipIsStyleAvailable() 进行了外部调用.

来自 ILSpy:

public bool IsStyleAvailable(FontStyle style)
{
int num2;
int num = SafeNativeMethods.Gdip.GdipIsStyleAvailable(new HandleRef(this, this.NativeFamily), style, out num2);
if (num != 0)
{
throw SafeNativeMethods.Gdip.StatusException(num);
}
return num2 != 0;
}

依次定义为:

[DllImport("gdiplus.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static extern int GdipIsStyleAvailable(HandleRef family, FontStyle style, out int isStyleAvailable);

关于c# - Windows 10 上的 .NET FontFamily 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33416741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com