我有一个类的以下代码。这是一个类的初始化。
第三方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上面的代码片段是我的代码,我使用TextDataProvider创建了一个TextDatProvider实例,并从API中调用了文本搜索。同样的代码在2小时内被调用超过1000次。对于不同的搜索字符串,文档ID都会调用。TextSearch的使用率很高。
如何解决此问题?这会不会是内存泄漏?我不能让它在测试或开发中发生。这是一个引用第三方组件的.NET应用程序。此代码是其组件的一部分。除了这个升级的第三方组件,什么都没有改变。
发布于 2019-08-14 06:54:37
另外,再来一个question..would应用程序池重置清除
对象?
您提到您的应用程序正在IIS中运行。当IIS AppPool被回收或超时(通常在20分钟后)时,IIS会卸载IIS应用程序的AppDomain。出于清理的目的,AppDomain有机会处理此事件。
对于ASP.NET应用程序,这将是Application_End方法。一定要在这里释放任何GDI对象(包括DC和字体)。
https://stackoverflow.com/questions/57437597
复制相似问题