首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CreateCompatibleDC( IntPtr.Zero )返回IntPtr.Zero

CreateCompatibleDC( IntPtr.Zero )返回IntPtr.Zero
EN

Stack Overflow用户
提问于 2019-08-10 06:02:06
回答 1查看 217关注 0票数 1

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

第三方DLL

代码语言:javascript
复制
 [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。

代码语言:javascript
复制
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的方式

代码语言:javascript
复制
#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应用程序。此代码是其组件的一部分。除了这个升级的第三方组件,什么都没有改变。

EN

回答 1

Stack Overflow用户

发布于 2019-08-14 06:54:37

另外,再来一个question..would应用程序池重置清除

对象?

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

对于ASP.NET应用程序,这将是Application_End方法。一定要在这里释放任何GDI对象(包括DC和字体)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57437597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档