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

标签 c# c++ gdi

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

第三方动态链接库

 [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/

相关文章:

c# - 更改屏幕的滤色器以与多个显示器一起使用

unit-testing - GDI中控制图的单元测试

delphi - 使用 Delphi XE2 的 AnimateWindow

c# - 在 C# 中混合错误和异常

调用 web api 时 C# 不支持授权类型

c++ - 为什么在程序终止时调用析构函数很重要?

使用 Stringstream 的 C++ Hex 到 Int

C# TCP 聊天服务器 : Connection only works one way

c# - .NET 6 升级导致 Azure Function 上的 DinkToPdf 损坏

c++ - 用空实现覆盖删除运算符