gpt4 book ai didi

c# - CreateCompatibleDC(IntPtr.Zero) 返回 IntPtr.Zero

转载 作者:行者123 更新时间:2023-11-30 04:45:03 29 4
gpt4 key购买 nike

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

第三方动态链接库

 [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 并抛出异常 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 片段是我使用 TextDataProvider 的代码我创建了一个 TextDatProvider 实例并从 API 调用文本搜索。同样的代码在 2 小时内被调用了 1000 多次。它被称为不同的搜索字符串,文档 ID。 TextSearch 被大量使用。

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

最佳答案

Also, one more question..would a App pool reset clear up GDI objects?

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

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

关于c# - CreateCompatibleDC(IntPtr.Zero) 返回 IntPtr.Zero,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57437597/

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