【问题标题】:CreateCompatibleDC(IntPtr.Zero) returns IntPtr.ZeroCreateCompatibleDC(IntPtr.Zero) 返回 IntPtr.Zero
【发布时间】: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 对象吗?

标签: c# c++ gdi


【解决方案1】:

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

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

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

【讨论】:

  • 没有您在评论中提到的工作进程的 GDI 对象。
  • @user575219 我从来没有说过“工作进程”。我说过你没有展示如何你正在破坏 GDI 资源。如果您使用已在 IIS 中分配的 GDI 资源调用此第 3 方 dll,则为 dangerous
  • 感谢您的澄清。我更新了帖子以显示我如何使用第三方 DLL。请提出建议。
  • 另外,我并不是说我的代码没有错误。但这是直到上个月才存在并且运行良好的代码。唯一改变的是第 3 方 DLL 的升级。
  • 感谢您的帮助。我告诉支持人员要考虑使用旧版本添加新 Vm。感谢您的帮助。
猜你喜欢
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 2010-11-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
相关资源
最近更新 更多