【发布时间】:2019-12-17 15:22:33
【问题描述】:
我有一个类的以下代码。这是一个类的初始化。
第三方 DLL
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
protected void initialize()
{
if (_initialized)
{
return;
}
if (_hdc == IntPtr.Zero)
{
_hdc = GDI32.CreateCompatibleDC(IntPtr.Zero);
if (_hdc == IntPtr.Zero)
{
throw new GDIException("Failed to create compatible device context.");
}
}
if (_hFontOld == IntPtr.Zero)
{
_hFont = FontSettings.GenerateHFont(_fontSetting, _hdc, _dpi, _forceFixedPitch);
_hFontOld = GDI32.SelectObject(_hdc, _hFont);
}
_initialized = true;
updateHeightAndWidth();
}
抱歉,我没有发布 Dispose。这里是!。这是一个第 3 方 DLL,在生产中每 3-4 小时导致一次此错误。我们公司使用这个第 3 方软件。在升级之前没有发生此错误。 第三方 DLL。
protected virtual void Dispose(bool isDisposing)
{
if (_isDisposed)
{
return;
}
releaseOldBitmap();
if (_hFont != IntPtr.Zero)
{
if (_hFontOld != IntPtr.Zero && _hdc != IntPtr.Zero)
{
GDI32.SelectObject(_hdc, _hFontOld);
}
if (GDI32.DeleteObject(_hFont))
{
_hFont = IntPtr.Zero;
}
}
if (_hdc != IntPtr.Zero && GDI32.DeleteDC(_hdc))
{
_hdc = IntPtr.Zero;
}
_isDisposed = true;
}
~TextPageRenderer()
{
Dispose(isDisposing: false);
}
public void Dispose()
{
Dispose(isDisposing: true);
GC.SuppressFinalize(this);
}
此代码在生产中运行良好。但是在服务器上的一些负载之后每隔 4 小时左右, GDI32.CreateCompatibleDC(IntPtr.Zero) 返回 IntPtr.Zero 并且抛出异常 throw new GDIException("Failed to create compatible device context.")
我们的代码:这就是我在代码中使用第 3 方 DLL 的方式
#region ExternalText
public static DocumentsList ExternalText(Application obApp, int? _RequestCount, int[] _ItemTypeIDs, KeywordIdPairs _Keywords, Constraints _Constraints)
{
var Results = new DocumentsList();
TextSearchResults textSearchResults;
var _SearchString = "";
DateTime startDate;
DateTime endDate;
long startDocumentId;
long endDocumentId;
var textSearchOptions = new TextSearchOptions();
var docQuery = obApp.Core.CreateDocumentQuery();
var textProvider = obApp.Core.Retrieval.Text;
try
{
var keywords = obApp.Core.KeywordTypes;
startDocumentId = 1;
endDocumentId = 10;
docQuery.AddDocumentRange(startDocumentId, endDocumentId);
var documentList = docQuery.Execute(Convert.ToInt32(_RequestCount));
_SearchString = "0916";
if (!String.IsNullOrEmpty(_SearchString))
{
foreach (var document in documentList)
{
var keyValueList = new KeyValueList<string, string>();
if (document != null && document.DefaultRenditionOfLatestRevision != null && document.DefaultRenditionOfLatestRevision.FileType != null && document.DefaultRenditionOfLatestRevision.FileType.Extension == "ctx")
{
textSearchResults = textProvider.TextSearch(document.DefaultRenditionOfLatestRevision, _SearchString, textSearchOptions);
foreach (var textSearchResult in textSearchResults)
{
var t = typeof(TextSearchItem);
PropertyInfo[] properties = t.GetProperties();
keyValueList.Add(ExternalTextRequest.DocID, document.ID.ToString());
keyValueList.Add(ExternalTextRequest.DocName, document.Name);
keyValueList.Add(ExternalTextRequest.DocumentType, document.DocumentType.Name);
foreach (PropertyInfo pi in t.GetProperties())
{
if (pi.Name == "SizeX")
{
keyValueList.Add(ExternalTextRequest.Width, pi.GetValue(textSearchResult, null).ToString());
}
else if (pi.Name == "SizeY")
{
keyValueList.Add(ExternalTextRequest.Height, pi.GetValue(textSearchResult, null).ToString());
}
}
Results.Add(keyValueList);
}
}
else
{
}
}
}
return Results;
}
catch (UnityAPIException e)
{
throw e;
}
catch (Exception ex)
{
throw ex;
}
return Results;
}
enter code here
aboce sn-p 是我使用 TextDataProvider 的代码 我创建了一个 TextDatProvider 实例并从 API 调用 textsearch。相同的代码在 2 小时内被调用超过 1000 次。它被称为不同的搜索字符串,文档ID。 TextSearch 被大量使用。
如何解决此问题。这可能是内存泄漏吗? 我无法在测试或开发中实现它。 这是一个引用第 3 方组件的 .NET 应用程序。此代码是其组件的一部分。除了这个升级的第 3 方组件外,没有任何变化。
【问题讨论】:
-
DC 是系统范围的有限资源,无论您拥有多少 RAM。您没有显示任何用于破坏 DC 和字体的代码。你是吗?
-
运行任务管理器,转到“详细信息”选项卡,选择“GDI 对象”。很可能您正在泄漏资源,在这种情况下,GDI 计数将继续上升(限制为 10,000)
-
除了@BarmakShemirani 的精彩评论,您可能还想查看SysInternals Process Explorer。在那里,您可以跟踪 GDI 句柄以及其他好东西
-
@MickyD:对不起,我发布了处理代码。感谢您的帮助和耐心。我正在使用 iLSpy 来反编译他们的 DLL。我想知道我是否有办法处理这个问题。
-
@BarmakShemirani:感谢您的意见。我在任务管理器中选择了它。这在服务器上可能不是客户端机器上。另外,还有一个问题..应用程序池重置会清除 GDI 对象吗?