CreateCompatibleDC(IntPtr.Zero) returns IntPtr.Zero

CreateCompatibleDC(IntPtr.Zero) returns IntPtr.Zero

本文关键字:Zero IntPtr returns CreateCompatibleDC      更新时间:2023-10-16

我有一个类的以下代码。这是类的初始化。

第三方 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();
}

对不起,我没有发布处置。在这里!。这是一个第三方 DLL,在生产中每 3-4 小时导致此错误。我们公司使用此第三方软件。此错误在升级之前未发生。 第三方 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,并抛出异常抛出新的 GDIException("无法创建兼容的设备上下文"。

我们的代码:这就是我在代码中使用第三方 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 片段是我使用 TextDataProvider 的代码 我创建了一个 TextDatProvider 的实例,并从 API 调用文本搜索。相同的代码在 2 小时内被调用超过 1000 次。它被调用不同的搜索字符串,文档 ID。文本搜索被大量使用。

如何解决此问题。这可能是内存泄漏吗? 我无法在测试或开发中实现它。 这是一个引用第三方组件的 .NET 应用程序。此代码是其组件的一部分。除了升级的第三方组件外,没有任何变化。

另外,还有一个问题..应用程序池重置是否会清除 GDI 对象?

你提到你的应用在 IIS 中运行。当 IIS 应用程序池被回收或超时(通常在 20 分钟后(时,IIS 会卸载 IIS 应用程序的 AppDomain。 应用程序域有机会处理此事件以进行清理。

对于 ASP.NET 应用程序,这将是Application_End方法。 请务必在此处释放任何 GDI 对象(包括 DC 和字体(。